如Twitter客户端或者HTC的日历应用,可以添加大小不同的Widget。此疑问。原来如此简单,只是我原来不会罢了。首先在AndroidManifest.xml中太假多个接收器:Xml代码
- <receiver android:name="com.jftt.widget.MyWidgetProvider1" android:label="天气 4 x 1">
- <meta-data android:name="android.appwidget.provider"
- android:resource="@xml/my_widget1" />
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- </intent-filter>
- </receiver>
- <receiver android:name="com.jftt.widget.MyWidgetProvider" android:label="全部 4 x 2">
- <meta-data android:name="android.appwidget.provider"
- android:resource="@xml/my_widget" />
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- </intent-filter>
- </receiver>
- <receiver android:name="com.jftt.widget.MyWidgetProvider2" android:label="步数 4 x 1">
- <meta-data android:name="android.appwidget.provider"
- android:resource="@xml/my_widget2" />
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- </intent-filter>
- </receiver>
CreateWidget.java文件内容如下:Java代码
- package com.jftt.activity;
-
- import android.app.Activity;
- import android.appwidget.AppWidgetManager;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
-
- import com.jftt.widget.R;
-
- public class CreateWidget extends Activity {
- private static final String TAG = "CreateWidget";
- int mAppWidgetId;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.i(TAG, " on WidgetConf ... ");
-
- setResult(RESULT_CANCELED);
- // Find the widget id from the intent.
- Intent intent = getIntent();
- Bundle extras = intent.getExtras();
- if (extras != null) {
- mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
- }
-
- // If they gave us an intent without the widget id, just bail.
- if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
- finish();
- }
-
- // return OK
- Intent resultValue = new Intent();
- resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
-
- setResult(RESULT_OK, resultValue);
- finish();
- }
- }