id桌面小插件——W

id桌面小插件——W

Android桌面小插件——Widget

效果图

Android桌面小插件——Widget-LMLPHP

实现

1. 创建Widget类

package com.kongqw.kqwwidget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; /**
* Created by kongqw on 2015/12/31.
*/
public class KqwWidget extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
} @Override
public void onDisabled(Context context) {
super.onDisabled(context);
} @Override
public void onEnabled(Context context) {
super.onEnabled(context);
} @Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
} @Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds); Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
ComponentName provider = new ComponentName(context.getApplicationContext(), KqwWidget.class);
/*
* 获取Widget远程View对象
* 参数1 Widget的包名
* 参数2 Widget的布局id
*/
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
// 获取远程View布局中的控件
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(date);
views.setTextViewText(R.id.textView, "时间:" + time); appWidgetManager.updateAppWidget(provider, views);
}
};
// 开启定时任务,每5秒更新执行一次
timer.schedule(timerTask, 1000, 1000);
} public void method(Context context) {
Toast.makeText(context, "KqwWidget里的方法", Toast.LENGTH_SHORT).show();
}
}

2. 在清单文件中注册Widget控件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kongqw.kqwwidget"> <application
……>
…… <receiver android:name=".KqwWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info" />
</receiver> </application>
</manifest>

3. 创建Widget配置文件

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/appwidget"
android:minHeight="72dp"
android:minWidth="294dp"
android:previewImage="@mipmap/ic_launcher"
android:updatePeriodMillis="86400000" />

Android桌面小插件——Widget-LMLPHP

4. 添加Widget布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
tools:context="com.kongqw.kqwwidget.MainActivity"> <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:gravity="center"
android:text="时间"
android:textColor="#FF000000"
android:textSize="30dp" />
</RelativeLayout>

调用Widget里的方法

KqwWidget kqwWidget = new KqwWidget();
kqwWidget.method(this);
05-07 09:52