1.什么是App widget
Application Widget 应用程序插件 可以放置于桌面
1.AppWidgetProviderInfo对象
为App Widget提供元数据,包括布局,更新频率等等数据,被定义于XML文件中;
2.AppWidgetProvider
定义了App Widget的基本生命周期
2.创建一个App Widget
1.定义一个AppWidgetProviderInfo
在res/xml文件夹中定义一个XML文件(demo_widget_info.xml)
2.在AndroidManifest.xml中添加接收者
3.为App Widget指定样式和布局(demo_widget.xml)
4.实现AppWidgetProvider(DemoWidgetProvider)
onUpdate:到达指定更新时间或被添加到桌面时调用方法
onDeleted:当插件被删除时调用方法
onEnabled:当第一个实例被创建时调用方法
onDisabled:当最后一个实例被移除时调用方法
onReveice:接收广播事件时调用方法
public class DemoWidgetProvider extends AppWidgetProvider
{
//...
}
3.什么是PendingIntent
1.一个延迟的Intent,用于交给其它进程,当其它进程事件触发后执行
2.创建方式
getActivity(Context context, int requestCode, Intent intent, int flags)
getBroadcast(Context context, int requestCode, Intent intent, int flags)
getService(Context context, int requestCode, Intent intent, int flags)
4.RemoteViews的作用
1.RemoteViews对象表示了一系列的View对象
2.RemoteViews所表示的对象运行在另外的进程当中
5.在AppWidget当中使用控件
1.在layout中绑定按钮
2.为按钮绑定监听器
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
Intent intent = new Intent(context, TargetActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(content.getPackageName(), R.layout.demo_widget);
remoteViews.setOnClickPendingIntent(R.id.btn, pIntent);
appWidgetManager.updateAppWidget(appWidgetIds[0], remoteViews);
}
6.接收来自AppWidget的广播
1.在AndroidManifest.xml当中为AppWidgetProvider注册新的intent-filter
2.使用getBroadcast()方法创建一个PendingIntent
3.为AppWidget当中的控件注册处理事件
private static final String UPDATE_ACTION = "dier.Study.UPDATE_APP_WIDGET";
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
Intent intent = new Intent(context, TargetActivity.class);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(content.getPackageName(), R.layout.demo_widget);
remoteViews.setOnClickPendingIntent(R.id.btn, pIntent);
appWidgetManager.updateAppWidget(appWidgetIds[0], remoteViews);
}
4.在onReceive方法当中接收广播信息
public void onReceive(Context context, Intent intent){
System.out.println("Action is : " + intent.getAction());
}
7.更新AppWidget当中控件的状态
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals(UPDATE_ACTION)){
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.demo_widget);
views.setTextViewText(R.id.btn, "test");
AppWidgetManager manager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, WidgetProvider.class);
manager.updateAppWidget(componentName, views);
}
else super.onReceive(context, intent);
}
//完整的示例
public class DemoWidgetProvider extends AppWidgetProvider {
private static final String UPDATE_ACTION = "dier.Study.WIDGET_UPDATE";
@Override
public void onEnabled(Context context) {
System.out.println("App Widget onEnabled ...");
super.onEnabled(context);
}
@Override
public void onDisabled(Context context) {
System.out.println("App Widget onDisabled ...");
super.onDisabled(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
System.out.println("App Widget onUpdate ...");
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intent = new Intent(context, WidgetActivity.class);
//PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
intent.setAction(UPDATE_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.demo_widget);
remoteViews.setOnClickPendingIntent(R.id.btnWidget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("Action is : " + action);
if (action.equals(UPDATE_ACTION)) {
System.out.println("App Widget onReceive ... [UPDATE_ACTION]");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.demo_widget);
//remoteViews.setString(R.id.btnWidget, "setText", "小亲亲");
remoteViews.setTextViewText(R.id.btnWidget, "小亲亲");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, DemoWidgetProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
} else {
System.out.println("App Widget onReceive ...");
super.onReceive(context, intent);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
System.out.println("App Widget onDeleted ...");
super.onDeleted(context, appWidgetIds);
}
}