安卓控件APPwidget 三

来源:互联网 发布:明穿小说 知乎 编辑:程序博客网 时间:2024/05/21 07:15

1 接收来自Appwidget的广播

1)、在AndroidManifest.xml为AppWidgetProvider注册新的intent

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="mars.appwidget03"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  <receiver android:name="ExampleAppWidgetProvider">
   <intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
   </intent-filter>
   <intent-filter>
    <action android:name="li.appwidget03.UPDATE_APP_WIDGET"/>   //和声明的常量updateAction对应
   </intent-filter>
   <meta-data android:name="android.appwidget.provider"
    android:resource="@xml/example_appwidget_info" />
  </receiver>
    </application>
    <uses-sdk android:minSdkVersion="7" />

</manifest>

2)、使用getBroadcast()方法创建一个pendingIntent;发送一个广播消息,用getActivity方法创建pendingIntent为了启动一个activity

//创建一个Intent对象
   Intent intent = new Intent();
   intent.setAction(UPDATE_ACTION);
   //使用getBoradCast方法创建一个PendingIntent,当对象执行时会发送广播
   PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
   RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
   //为按钮绑定事件处理器
   //第一个参数用来指定被绑定处理器的控件的ID
   //第二个参数用来指定当事件发生时,哪个PendingIntent将会被执行
   remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
   //更新AppWidget
   //第一个参数用于指定被更新AppWidget的ID
   appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
 
  super.onUpdate(context, appWidgetManager, appWidgetIds);

如果是更改状态的话

Intent intent = new Intent();
  //为Intent对象设置Action
  intent.setAction(UPDATE_ACTION);
  //使用getBroadcast方法,得到一个PendingIntent对象,当该对象执行时,会发送一个广播
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
    intent, 0);

3)、为Appwidget当中的控件注册处理器

<intent-filter>
    <action android:name="li.appwidget03.UPDATE_APP_WIDGET"/>   //和声明的常量updateAction对应
   </intent-filter>

4)、在onreceive方法当中接受广播消息

AppWidgetProvider--Intent---onReceive:onUpdate,onDeleted,onDisabled,onEnabled四个action

super.onReceive(context, intent);
  String action=intent.getAction();
  if(UPDATE_ACTION.equals(action)){
   System.out.println("onReceive"+UPDATE_ACTION);
  }

如果是更改APPwidget的状态则用下面代码接收

super.onReceive(context, intent);
  String action = intent.getAction();
  if (UPDATE_ACTION.equals(action)) {
   //remoteViews 代表里面控件
   RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
     R.layout.example_appwidget);
   remoteViews.setImageViewResource(R.id.imageId,R.drawable.qie);
   remoteViews.setTextViewText(R.id.widgetTextId, "text");
  
   AppWidgetManager appWidgetmanager = AppWidgetManager.getInstance(context);
   //compon代表整个APPwidget
   ComponentName componentName = new ComponentName(context,ExampleAppWidgetProvider.class);
   appWidgetmanager.updateAppWidget(componentName, remoteViews);
  } else
  {
   super.onReceive(context,intent);
  }

注意:当layout中xml文件有不对的时候,android的R文件是不能生成的,这时候点击project--clean文件R丢失,修改layout文件后 R文件会重新生成。