Android开发之接受来自Appwidget的广播、更新Appwidget控件的状态

原文链接: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方法

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <ImageButton   
  7.         android:id="@+id/imageButton1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@drawable/th_desktop"/>  
  11.     <ImageView  
  12.         android:id="@+id/imageView1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:src="@drawable/th_twitter"  
  16.         />  
  17. </LinearLayout>  


 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:minWidth="292dp"  
  4.     android:minHeight="72dp"  
  5.     android:updatePeriodMillis="30000"  
  6.     android:initialLayout="@layout/main"  
  7.     >  
  8. </appwidget-provider>  


 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <receiver android:name="AppWidget">  
  2.            <intent-filter>  
  3.                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>  
  4.            </intent-filter>  
  5.            <intent-filter>  
  6.                <action android:name="org.wwj.appwidget.UPDATE_APP_WIDGET"/>  
  7.            </intent-filter>  
  8.            <meta-data android:name="android.appwidget.provider"  
  9.                android:resource="@xml/appwidget_provider"/>  
  10.        </receiver>  


 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package mars.appwidget03;  
  2.   
  3. import android.app.PendingIntent;  
  4. import android.appwidget.AppWidgetManager;  
  5. import android.appwidget.AppWidgetProvider;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.widget.RemoteViews;  
  9.   
  10. public class AppWidget extends AppWidgetProvider {  
  11.     //定义一个常量字符串,该常量用于命名Action  
  12.     private static final String UPDATE_ACTION = "mars.appwidget03.UPDATE_APP_WIDGET";  
  13.   
  14.     @Override  
  15.     public void onDeleted(Context context, int[] appWidgetIds) {  
  16.         // TODO Auto-generated method stub  
  17.         super.onDeleted(context, appWidgetIds);  
  18.     }  
  19.   
  20.     @Override  
  21.     public void onDisabled(Context context) {  
  22.         // TODO Auto-generated method stub  
  23.         super.onDisabled(context);  
  24.     }  
  25.   
  26.     @Override  
  27.     public void onEnabled(Context context) {  
  28.         // TODO Auto-generated method stub  
  29.         super.onEnabled(context);  
  30.     }  
  31.   
  32.     @Override  
  33.     public void onReceive(Context context, Intent intent) {  
  34.         super.onReceive(context, intent);  
  35.         String action = intent.getAction();  
  36.         if (UPDATE_ACTION.equals(action)) {  
  37.             System.out.println("onReceive--->" + UPDATE_ACTION);  
  38.         }  
  39.     }  
  40.   
  41.     @Override  
  42.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
  43.             int[] appWidgetIds) {  
  44.         //创建一个Intent对象  
  45.         Intent intent = new Intent();  
  46.         //为Intent对象设置Action  
  47.         intent.setAction(UPDATE_ACTION);  
  48.         //使用getBroadcast方法,得到一个PendingIntent对象,当该对象执行时,会发送一个广播  
  49.         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,  
  50.                 intent, 0);  
  51.         RemoteViews remoteViews = new RemoteViews(context.getPackageName(),  
  52.                 R.layout.main);  
  53.         remoteViews.setOnClickPendingIntent(R.id.imageButton, pendingIntent);  
  54.         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);  
  55.     }  
  56.   
  57. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值