zoukankan      html  css  js  c++  java
  • AppWidget应用(三)---PendingIntent 之 getBroadcast

    下面我们来看下appWidget如何通过广播来更新appWidget上的信息,在AppWidget应用(二)的基础上,需要添加一个自定义的消息,并且在AndriodMainfest上注册;代码如下

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.example.appwidgetdemo"  
    4.     android:versionCode="1"  
    5.     android:versionName="1.0" >  
    6.   
    7.     <uses-sdk  
    8.         android:minSdkVersion="8"  
    9.         android:targetSdkVersion="17" />  
    10.   
    11.     <application  
    12.         android:allowBackup="true"  
    13.         android:icon="@drawable/ic_launcher"  
    14.         android:label="@string/app_name"  
    15.         android:theme="@style/AppTheme" >  
    16.         <activity  
    17.             android:name="com.example.appwidgetdemo.MainActivity"  
    18.             android:label="@string/app_name" >  
    19.             <intent-filter>  
    20.                 <action android:name="android.intent.action.MAIN" />  
    21.   
    22.                 <category android:name="android.intent.category.LAUNCHER" />  
    23.             </intent-filter>  
    24.         </activity>  
    25.   
    26.         <receiver android:name="com.example.appwidgetdemo.appWidgetActivity" >  
    27.             <intent-filter>  
    28.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" >  
    29.                 </action>  
    30.             </intent-filter>  
    31.   
    32.             <meta-data  
    33.                 android:name="android.appwidget.provider"  
    34.                 android:resource="@xml/appwidget01" />  
    35.   
    36.             <!-- 注册要处理的消息 -->  
    37.             <span style="color:#ff0000;"><intent-filter>  
    38.                 <action android:name="com.example.appWidgetUpdate" >  
    39.                 </action>  
    40.             </intent-filter></span>  
    41.         </receiver>  
    42.     </application>  
    43.   
    44. </manifest>  

    广播消息定义为:private static final String UPDATE_RECEIVE = "com.example.appWidgetUpdate";


    然后重载AppWidgetProvider中的几个方法

    [java] view plaincopy
     
    1. /** 
    2.      * 接受广播事件 
    3.      */  
    4.     @Override  
    5.     public void onReceive(Context context, Intent intent) {  
    6.         // TODO Auto-generated method stub  
    7.         String Msg = intent.getAction();  
    8.         //处理我们自定义的消息  
    9.         if (Msg.equals(UPDATE_RECEIVE)) { // 判断广播如果为:com.qlf.appWidgetUpdate才处理  
    10.             System.out.println("----------------onReceive");  
    11.             // 只能通过远程对象来设置appwidget中的控件状态  
    12.             RemoteViews remoteViews = new RemoteViews(context.getPackageName(),  
    13.                     R.layout.appwidgetlayout);  
    14.             //改变指定控件的值  
    15.             remoteViews.setTextViewText(R.id.txtapp, "我变-hihi");  
    16.             remoteViews.setImageViewResource(R.id.image, R.drawable.local_file);  
    17.   
    18.             // 获得appwidget管理实例,用于管理appwidget以便进行更新操作  
    19.             AppWidgetManager appWidgetManager = AppWidgetManager  
    20.                     .getInstance(context);  
    21.   
    22.             // 相当于获得所有本程序创建的appwidget  
    23.             ComponentName componentName = new ComponentName(context,  
    24.                     appWidgetActivity.class);  
    25.   
    26.             // 更新appwidget  
    27.             appWidgetManager.updateAppWidget(componentName, remoteViews);  
    28.         }  
    29.         //处理系统发送的消息  
    30.         else{  
    31.             super.onReceive(context, intent);  
    32.         }  
    33.     }  
    34.   
    35.     /** 
    36.      * 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用 
    37.      */  
    38.     @Override  
    39.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
    40.             int[] appWidgetIds) {  
    41.         System.out.println("----------------onUpdate");  
    42.         // TODO Auto-generated method stub  
    43.         // 创建一个Intent对象  
    44.         Intent intent = new Intent();  
    45.         intent.setAction(UPDATE_RECEIVE);  
    46.         // 这里是getActivity,当然也可以是broadcastReceiver等   发送一个广播消息  
    47.         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,  
    48.                 intent, 0);  
    49.         RemoteViews remoteViews = new RemoteViews(context.getPackageName(),  
    50.                 R.layout.appwidgetlayout);  
    51.   
    52.         //为按钮绑定监听器  
    53.         remoteViews.setOnClickPendingIntent(R.id.btnSend, pendingIntent);  
    54.   
    55.         // 更新Appwidget  
    56.         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);  
    57.     }  


    启动后的效果如图所示:

    点击按钮后TextView 和 ImageView都改变了

    照旧附上源码

    点击打开链接

    原文:http://blog.csdn.net/deng0zhaotai/article/details/10414285

  • 相关阅读:
    设计模式之原型模式
    【转载】 吵翻了!这张图里到底是人还是狗?心理学家这样说
    【转载】 DeepMind 提出元梯度强化学习算法,显著提高大规模深度强化学习应用的性能
    ubuntu18.04 安装wine64出现错误: X 64-bit development files not found.
    ubuntu18.04 源码方式安装wine , 警告,libxrender 64-bit development files not found, XRender won't be supported.
    【转载】 信息如何像零食、金钱一样掌控你的大脑
    图像处理算法 之 滤波 模糊(基于OpenCV)
    图像处理之FPN校正
    ISP-黑电平校正(BLC)
    ISP基础(0y):图像传感器
  • 原文地址:https://www.cnblogs.com/veins/p/3831778.html
Copyright © 2011-2022 走看看