App Widget简单应用

  首先后台进程创建一个PendingIntent对象,其中PendingIntent中包含一个真正的Intent,创建完成后将此PendingIntent对象交给桌面控件所在的进程,当用户点击桌面控件或者其他情况时,触发Intent,从而可实现启动一个Activity、发送一个Broadcast、启动一个Service。

  创建PendingIntent的方法,其中的三个方法都是PendingIntent的静态方法:

1、getActivity(Context context,int requestCode,Intent intent,int flags)

2、getBroadcast(Context context,int requestCode,Intent intent,int flags)

3、getService(Context context,int requestCode,Intent intent,int flags)

  RemoteViews的作用

    1、RemoteViews对象表示一系列的View对象

           2、RemoteViews所表示的对象运行在另外的进程当中

  向App Widget中添加Button并为Button绑定监听事件:

    首先在example_appwidget.xml添加Button按钮:

      <Button
        android:id="@+id/ButtonId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试按钮"/>

    为Button绑定处理器:

                 由于App Widget和应用程序运行在不同的进程当中(App Widget当中的View运行在Home Screen进程当中),所以无法按照之前的惯用方法绑定监听器。

                  remoteViews.setOnClickPendingIntent(R.id.ButtonId, pendingIntent);

           在ExampleAppWidgetProvider类(extends AppWidgetProvider)的onUpdate()方法:

      <1>点击Button启动一个Activity:

        for(int i = 0;i<appWidgetIds.length;i++){
          //创建一个Intent对象
          Intent intent = new Intent(context,MainActivity.class);
          //创建一个PendingIntent
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
          RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
          //为按钮绑定事件处理器
          //第一个参数用来指定被绑定处理器的控件的ID
          //第二个参数用来指定当事件发生时,哪个PendingIntent将会被执行
          remoteViews.setOnClickPendingIntent(R.id.ButtonId, pendingIntent);
          //更新AppWidget
          //第一个参数用于指定被更新AppWidget的ID
          appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        }

      <2>点击Button发送一个广播事件,更新App Widget控件内容:

                       首先在AndroidManifest.xml文件中添加一个action,当intent包含此action时都可以被ExampleAppWidgetProvider类(extends AppWidgetProvider)接收

          <receiver android:name="ExampleAppWidgetProvider">
            <intent-filter >
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            <intent-filter >
              <action android:name="zhanglei.appwidget.UPDATE_APP_WIDGET"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
              android:resource="@xml/example_appwidget_info"/>
          </receiver>

      ExampleAppWidgetProvider类代码如下:

        //定义一个常量字符串,该常量用于命名Action
        private static final String UPDATE_ACTION = "zhanglei.appwidget.UPDATE_APP_WIDGET";

                      onUpdate()代码如下:

          //创建一个Intent对象
          Intent intent = new Intent();
          intent.setAction(UPDATE_ACTION);
          //使用getBroadcast方法,得到一个PendingIntent对象,当该对象执行时会发送一个广播
          PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
          RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
          remoteViews.setOnClickPendingIntent(R.id.ButtonId, pendingIntent);
          appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
          // TODO Auto-generated method stub
          super.onUpdate(context, appWidgetManager, appWidgetIds);

        调用RemoteViews类当中的方法更新控件:

          public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            super.onReceive(context, intent);
            String action = intent.getAction();
            if(UPDATE_ACTION.equals(action)){
              //remoteViews代表整个Widget中的所有控件
              RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
              //第一个参数是ImageView控件的ID
              //第二个参数是图片的ID
              remoteViews.setImageViewResource(R.id.ImageId, R.drawable.ic_launcher);
              remoteViews.setTextViewText(R.id.textId, "text");
              //得到AppWidgetManager对象
              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
              //componentName代表整个Widget
              ComponentName componentNane = new ComponentName(context,ExampleAppWidgetProvider.class);
              appWidgetManager.updateAppWidget(componentNane, remoteViews);
            }
            else{ //处理其他的action,调用父类中的onReceive方法
              super.onReceive(context, intent);
            }
          }

原文地址:https://www.cnblogs.com/zhanglei93/p/4756421.html