1. 接受来自App Widget的广播

     //Androidmanifest中的文件旨要,只要有个一action符合就会调用继承AppWidgetProvider的类:

     <receiver android:name="继承AppWidgetProvider的类">

     <intent-filter>

        <action android:name="android.appwidget.action.APPWIDGET_UPDATA"/>

     </intent-filter>

     <intent-filter>

        <action android:name="mars.appwidget03.UPDATE_APP_WIDGET"/>      //这是自定义action

     </intent-filter>

     <meta-data android:name="android.appwidget.provider"

          android:resource="@xml/example_appwidget_info"/>

     </receiver>

    //在代码中继承AppWidgetProvider的类的程况如下:

     A. 在Update方法中

     Intent intent = new Intent();

     intent.setAction("mars.appwidget03.UPDATE_APP_WIDGET");

     使用getBroadcast方法得到PendingIntent对象

     PendingIntent pendingIntent = PendingIntent.getBroadcast(contect,0,intent对象,0);

     B. 再onReciever方法中接收步骤A发送的广播

    

    2. 更新App Widget当中控件的状态

    public void onReceive(Context context, Intent intent){

    String action = intent.getAction();

    if(action == "mars.appwidget03.UPDATE_APP_WIDGET"){

      RemoteViews remoteViews = new RemoteViews(context.getPackage , R.layout.example_appwidget);

      //修改图片

      remoteViews .setImageViewResource(R.id.imageId , R.drawable.ku);

      remoteViews 。setTextViewText(R.id.widgetTextId , "");

      //获得AppWidgetManager 对象

      AppWidgetManagerappWidgetManager =AppWidgetManager.getInstance(context);

      //ComponentName指的是整个app widget控件 , 而RemoteViews指的是app widget里面的控件

      ComponentNamecomponentName = newComponentName(context, ExampleAppWidgetProvider.class);

      //更新appwidget

      appWidgetManager .updateAppWidget(componentName , remoteViews);

        }

        //必须要有else语句,不然其他的onUpdate,onEnable等函数将不会调用

        else{

        superonReceive(context,intent);

          }

      }

更多相关文章

  1. location of the android sdk has not been setup in the prefer
  2. wm命令使用方法(修改android 分辨率)修改
  3. android中在子线程中更新UI的几种方法
  4. Android Studio调错误方法
  5. Android动态设置Margin的方法
  6. 完美解决Android Studio在写XML布局的时候没有了控件代码提示的
  7. Android 子控件高度超出父布局的限制

随机推荐

  1. 谁说Android的动画不廉价(五)之水波纹动画
  2. 【Android】多语言Values
  3. android之spinner背景、字体颜色
  4. Android动态加载jar/dex
  5. Android : 输入设备键值从底层到应用层的
  6. Android之SurfaceView简介(三)
  7. Android脚本插件系列(一):安卓国际化多语
  8. Android 应用程序基础(Application Fundam
  9. Android(安卓)后台服务
  10. Android入门:使用Android自带媒体库读取MP