原文链接:http://blog.csdn.net/wwj_748/article/details/7872278
Android-接受来自Appwidget的广播、更新Appwidget控件的状态
听说得桌面者得天下,就拿PC来说吧,360和QQ基本上是使用最频繁的应用程序,每个程序在PC桌面右下角都会有相应的控件,占据桌面的时间越长,用户使用频率就越多,这样才会为应用程序带来更多的利益。手机桌面也是这样,毋庸置疑,使用最多自然是桌面上的。那如何添加自己的控件到桌面上呢,又如何改变控件的状态呢。
我自己做了个简单的实例:当点击图片按钮是,下面的图片就会更新为另一个图片
创建项目:AppWidget03
项目运行效果:
步骤:
1.定义布局文件:appwidget_provider_layout.xml
2.在res目录下新建目录xml,创建xml文件:appwidget_provider.xml
3.在manifest文件下注册receiver
4.新建类Appwidget继承AppwidgetProvider
5.重写AppwidgetProvider的OnUpdate方法,和OnReceiver方法
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ImageButton
- android:id="@+id/imageButton1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/th_desktop"/>
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/th_twitter"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="292dp"
- android:minHeight="72dp"
- android:updatePeriodMillis="30000"
- android:initialLayout="@layout/main"
- >
- </appwidget-provider>
- <receiver android:name="AppWidget">
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
- </intent-filter>
- <intent-filter>
- <action android:name="org.wwj.appwidget.UPDATE_APP_WIDGET"/>
- </intent-filter>
- <meta-data android:name="android.appwidget.provider"
- android:resource="@xml/appwidget_provider"/>
- </receiver>
- package mars.appwidget03;
- import android.app.PendingIntent;
- import android.appwidget.AppWidgetManager;
- import android.appwidget.AppWidgetProvider;
- import android.content.Context;
- import android.content.Intent;
- import android.widget.RemoteViews;
- public class AppWidget extends AppWidgetProvider {
- //定义一个常量字符串,该常量用于命名Action
- private static final String UPDATE_ACTION = "mars.appwidget03.UPDATE_APP_WIDGET";
- @Override
- public void onDeleted(Context context, int[] appWidgetIds) {
- // TODO Auto-generated method stub
- super.onDeleted(context, appWidgetIds);
- }
- @Override
- public void onDisabled(Context context) {
- // TODO Auto-generated method stub
- super.onDisabled(context);
- }
- @Override
- public void onEnabled(Context context) {
- // TODO Auto-generated method stub
- super.onEnabled(context);
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- super.onReceive(context, intent);
- String action = intent.getAction();
- if (UPDATE_ACTION.equals(action)) {
- System.out.println("onReceive--->" + UPDATE_ACTION);
- }
- }
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- //创建一个Intent对象
- Intent intent = new Intent();
- //为Intent对象设置Action
- intent.setAction(UPDATE_ACTION);
- //使用getBroadcast方法,得到一个PendingIntent对象,当该对象执行时,会发送一个广播
- PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
- intent, 0);
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
- R.layout.main);
- remoteViews.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
- appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
- }
- }