AppWidget应用(三)---PendingIntent 之 getBroadcast
2013-08-29 08:20
471 查看
下面我们来看下appWidget如何通过广播来更新appWidget上的信息,在AppWidget应用(二)的基础上,需要添加一个自定义的消息,并且在AndriodMainfest上注册;代码如下
[html] view
plaincopy
<?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" />
<!-- 注册要处理的消息 -->
<span style="color:#ff0000;"><intent-filter>
<action android:name="com.example.appWidgetUpdate" >
</action>
</intent-filter></span>
</receiver>
</application>
</manifest>
广播消息定义为:private static final String UPDATE_RECEIVE = "com.example.appWidgetUpdate";
然后重载AppWidgetProvider中的几个方法
[java] view
plaincopy
/**
* 接受广播事件
*/
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String 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);
// 相当于获得所有本程序创建的appwidget
ComponentName componentName = new ComponentName(context,
appWidgetActivity.class);
// 更新appwidget
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
//处理系统发送的消息
else{
super.onReceive(context, intent);
}
}
/**
* 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用
*/
@Override
public 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);
// 更新Appwidget
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
启动后的效果如图所示:
点击按钮后TextView 和 ImageView都改变了
照旧附上源码
点击打开链接
[html] view
plaincopy
<?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" />
<!-- 注册要处理的消息 -->
<span style="color:#ff0000;"><intent-filter>
<action android:name="com.example.appWidgetUpdate" >
</action>
</intent-filter></span>
</receiver>
</application>
</manifest>
广播消息定义为:private static final String UPDATE_RECEIVE = "com.example.appWidgetUpdate";
然后重载AppWidgetProvider中的几个方法
[java] view
plaincopy
/**
* 接受广播事件
*/
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String 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);
// 相当于获得所有本程序创建的appwidget
ComponentName componentName = new ComponentName(context,
appWidgetActivity.class);
// 更新appwidget
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
//处理系统发送的消息
else{
super.onReceive(context, intent);
}
}
/**
* 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用
*/
@Override
public 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);
// 更新Appwidget
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
启动后的效果如图所示:
点击按钮后TextView 和 ImageView都改变了
照旧附上源码
点击打开链接
相关文章推荐
- AppWidget应用(三)---PendingIntent 之 getBroadcast
- AppWidget应用(四)---PendingIntent 之 getService
- AppWidget应用(二)---PendingIntent 之 getActivity
- AppWidget应用(二)---PendingIntent 之 getActivity
- AppWidget应用(二)---PendingIntent 之 getActivity
- 【起航计划 017】2015 起航计划 Android APIDemo的魔鬼步伐 16 App->Alarm->Alarm Controller Alarm事件 PendingIntent Schedule AlarmManager
- android.app.PendingIntent
- Android AppWidget 开发中PendingIntent传送数据丢失解决办法
- AppWidget应用(三)---PendingIntent 之 getBroadcast
- 总结:AppWidget Notification PendingIntent Activity...
- PendingIntent android.app.PendingIntent.getActivity的使用
- Android:AppWidget,PendingIntent,RemoteViews用法
- 【起航计划 017】2015 起航计划 Android APIDemo的魔鬼步伐 16 App-&gt;Alarm-&gt;Alarm Controller Alarm事件 PendingIntent Schedule AlarmManager
- (转)Android:AppWidget,PendingIntent,RemoteViews用法
- Android PendingIntent的getActivity()、getBroadcast()、getService()方法的区别
- Android:AppWidget,PendingIntent,RemoteViews用法
- Android AppWidget 开发中PendingIntent传送数据丢失解决办法
- Android浏览器打开本地app前端同学的两种配置方式Url scheme和Android intent
- PendingIntent与Intent的区别
- Intent 和 PendingIntent 区别 .