android中的appwidget理解巩固
2012-10-13 20:55
162 查看
public class MyAppWidget extends AppWidgetProvider{
private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("-------------onDeleted----------------");
super.onDeleted(context, appWidgetIds);
}
//当最后一个appwidget删除后
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
System.out.println("-------------onDisabled----------------");
super.onDisabled(context);
}
//当第一次被创建时
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
System.out.println("-------------onEnabled----------------");
super.onEnabled(context);
}
//接收广播
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String actionString = intent.getAction();
System.out.println("-------------onReceive----------------"+actionString);
if(CLICK_ACTION.equals(actionString)){
System.out.println("===============");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
remoteViews.setTextViewText(R.id.text01, context.getResources().getString(R.string.hello_world));
remoteViews.setViewVisibility(R.id.button01, View.INVISIBLE);
Toast.makeText(context, "success", Toast.LENGTH_SHORT).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, MyAppWidget.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
}else{
super.onReceive(context, intent);
}
}
//用于更新操作
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("-------------onUpdate----------------");
Intent intent = new Intent();
intent.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
rViews.setOnClickPendingIntent(R.id.button01, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, rViews);
}
}
在manifest文件里面
<receiver android:name="MyAppWidget" android:exported="false">
<!-- 所需要满足的过滤器 -->
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="cxd.appwidget.BUTTON_ONCLICK" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget01_info" />
</receiver>
学了这么久,总结点appwidget的自己理解的东西记录下来!
执行顺序:
首先-->在桌面创建appwidget桌面小程序,
重点是 <intent-filter>
<action android:name="cxd.appwidget.BUTTON_ONCLICK" />
</intent-filter>
和代码里的Intent intent = new Intent();
intent.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);和String actionString = intent.getAction();
intent.setAction(CLICK_ACTION);是将private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";设置action来执行
然后和manifest里的 <intent-filter>
<action android:name="cxd.appwidget.BUTTON_ONCLICK" />
</intent-filter>比对,如果相匹配就为pendingIntent对象的广播事件设置这个action,所以在onReceive里面就用String actionString = intent.getAction();得到
在manifest匹配的action对象
还有种比较简单的使用getActivity(Context context, int requestCode, Intent intent, int flags); 创建PendingIntent对象
其中的intent对象是通过Intent intent = new Intent(context,XXX.class);来创建的,可以开启一个activity
private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("-------------onDeleted----------------");
super.onDeleted(context, appWidgetIds);
}
//当最后一个appwidget删除后
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
System.out.println("-------------onDisabled----------------");
super.onDisabled(context);
}
//当第一次被创建时
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
System.out.println("-------------onEnabled----------------");
super.onEnabled(context);
}
//接收广播
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String actionString = intent.getAction();
System.out.println("-------------onReceive----------------"+actionString);
if(CLICK_ACTION.equals(actionString)){
System.out.println("===============");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
remoteViews.setTextViewText(R.id.text01, context.getResources().getString(R.string.hello_world));
remoteViews.setViewVisibility(R.id.button01, View.INVISIBLE);
Toast.makeText(context, "success", Toast.LENGTH_SHORT).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, MyAppWidget.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
}else{
super.onReceive(context, intent);
}
}
//用于更新操作
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("-------------onUpdate----------------");
Intent intent = new Intent();
intent.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
rViews.setOnClickPendingIntent(R.id.button01, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, rViews);
}
}
在manifest文件里面
<receiver android:name="MyAppWidget" android:exported="false">
<!-- 所需要满足的过滤器 -->
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="cxd.appwidget.BUTTON_ONCLICK" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget01_info" />
</receiver>
学了这么久,总结点appwidget的自己理解的东西记录下来!
执行顺序:
首先-->在桌面创建appwidget桌面小程序,
重点是 <intent-filter>
<action android:name="cxd.appwidget.BUTTON_ONCLICK" />
</intent-filter>
和代码里的Intent intent = new Intent();
intent.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);和String actionString = intent.getAction();
intent.setAction(CLICK_ACTION);是将private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";设置action来执行
然后和manifest里的 <intent-filter>
<action android:name="cxd.appwidget.BUTTON_ONCLICK" />
</intent-filter>比对,如果相匹配就为pendingIntent对象的广播事件设置这个action,所以在onReceive里面就用String actionString = intent.getAction();得到
在manifest匹配的action对象
还有种比较简单的使用getActivity(Context context, int requestCode, Intent intent, int flags); 创建PendingIntent对象
其中的intent对象是通过Intent intent = new Intent(context,XXX.class);来创建的,可以开启一个activity
相关文章推荐
- android中的appwidget理解巩固
- 理解与应用Android桌面组件AppWidget
- 理解与应用Android桌面组件AppWidget
- android学习记录(十八)---AppWidget显示内容随数据改变而更新
- Android桌面小部件AppWidget(2)
- Android中Appwidget桌面小控件的简单入手方式
- android添加默认appwidget
- Android中的AppWidget入门教程
- Android中选取并绑定AppWidget
- Android AppWidget实例验证
- android launcher桌面数字时钟及appwidget更新问题
- Android中的AppWidget
- Android 之窗口小部件详解--App Widget
- Android开发之App Widget(二)
- Android窗口小部件APP Widget
- Android关机AppWidget的实现
- Android—AppWidget
- Android app widget 支持的Layout和widget组件
- Android 第三方桌面,怎么请求Widget的android.permission.BIND_APPWIDGET
- Android 之窗口小部件高级篇--App Widget 之 RemoteViews