android中的appwidget理解
2012-10-13 21:09:58 来源:WEB开发网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
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接