AppWidget应用(三)---PendingIntent 之 getBroadcast
来源:互联网 发布:linux rm 编辑:程序博客网 时间:2024/06/06 00:43
下面我们来看下appWidget如何通过广播来更新appWidget上的信息,在AppWidget应用(二)的基础上,需要添加一个自定义的消息,并且在AndriodMainfest上注册;代码如下
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.appwidgetdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.appwidgetdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.example.appwidgetdemo.appWidgetActivity" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" > </action> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget01" /> <!-- 注册要处理的消息 --> <intent-filter> <action android:name="com.example.appWidgetUpdate" > </action> </intent-filter> </receiver> </application></manifest>
然后重载AppWidgetProvider中的几个方法
/** * 接受广播事件 */@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubString Msg = intent.getAction();//处理我们自定义的消息if (Msg.equals(UPDATE_RECEIVE)) { // 判断广播如果为:com.qlf.appWidgetUpdate才处理System.out.println("----------------onReceive");// 只能通过远程对象来设置appwidget中的控件状态RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.appwidgetlayout);//改变指定控件的值remoteViews.setTextViewText(R.id.txtapp, "我变-hihi");remoteViews.setImageViewResource(R.id.image, R.drawable.local_file);// 获得appwidget管理实例,用于管理appwidget以便进行更新操作AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);// 相当于获得所有本程序创建的appwidgetComponentName componentName = new ComponentName(context,appWidgetActivity.class);// 更新appwidgetappWidgetManager.updateAppWidget(componentName, remoteViews);}//处理系统发送的消息else{super.onReceive(context, intent);}}/** * 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用 */@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {System.out.println("----------------onUpdate");// TODO Auto-generated method stub// 创建一个Intent对象Intent intent = new Intent();intent.setAction(UPDATE_RECEIVE);// 这里是getActivity,当然也可以是broadcastReceiver等 发送一个广播消息PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,intent, 0);RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.appwidgetlayout);//为按钮绑定监听器remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);// 更新AppwidgetappWidgetManager.updateAppWidget(appWidgetIds, remoteViews);}
启动后的效果如图所示:
点击按钮后TextView 和 ImageView都改变了
照旧附上源码
点击打开链接
- AppWidget应用(三)---PendingIntent 之 getBroadcast
- AppWidget应用(三)---PendingIntent 之 getBroadcast
- AppWidget应用(二)---PendingIntent 之 getActivity
- AppWidget应用(四)---PendingIntent 之 getService
- AppWidget应用(二)---PendingIntent 之 getActivity
- AppWidget应用(四)---PendingIntent 之 getService
- android PendingIntent.getBroadcast 之坑
- 巨坑,PendingIntent.getBroadcast
- PendingIntent在AppWidget程序中的应用
- PendingIntent在AppWidget程序中的应用
- PendingIntent在AppWidget程序中的应用
- Android开发之AppWidget应用(一)
- Android开发之AppWidget应用(二)
- Android开发之AppWidget应用(一)
- Android开发之AppWidget应用(二)
- Android开发之AppWidget应用(一)
- AppWidget(二) PendingIntent
- android应用开发(三)—— Appwidget
- VI常用命令
- c++继承中的内存布局
- Linux的多任务编程-线程
- sql之内外连接
- C++内存对象大会战
- AppWidget应用(三)---PendingIntent 之 getBroadcast
- hdu 1281 棋盘游戏 ( 行列匹配+求关键点 )
- Ubuntu创建一个可ssh可sudo的用户
- uva 127 - Accordian Patience
- java中的char占几个字节
- sql时间格式化
- java synchronized详解
- printf("%*.*s/n",m,n,ch);
- Power BI for Office 365(四)Power View第一部分