一个Android应用向Home screen添加多个Widget


如Twitter客户端或者HTC的日历应用,可以添加大小不同的Widget。此疑问。

原来如此简单,只是我原来不会罢了。

首先在AndroidManifest.xml中太假多个接收器:

Xml代码

  1. <receiver android:name="com.jftt.widget.MyWidgetProvider1" android:label="天气 4 x 1">  
  2.     <meta-data android:name="android.appwidget.provider"  
  3.             android:resource="@xml/my_widget1" />  
  4.     <intent-filter>  
  5.         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />     
  6.     </intent-filter>  
  7. </receiver>  
  8. <receiver android:name="com.jftt.widget.MyWidgetProvider" android:label="全部 4 x 2">  
  9.     <meta-data android:name="android.appwidget.provider"  
  10.             android:resource="@xml/my_widget" />  
  11.     <intent-filter>  
  12.         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />     
  13.     </intent-filter>  
  14. </receiver>  
  15. <receiver android:name="com.jftt.widget.MyWidgetProvider2" android:label="步数 4 x 1">  
  16.     <meta-data android:name="android.appwidget.provider"  
  17.             android:resource="@xml/my_widget2" />  
  18.     <intent-filter>  
  19.         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />     
  20.     </intent-filter>  
  21. </receiver>  

CreateWidget.java文件内容如下:

Java代码

  1. package com.jftt.activity;   
  2.   
  3. import android.app.Activity;   
  4. import android.appwidget.AppWidgetManager;   
  5. import android.content.Intent;   
  6. import android.os.Bundle;   
  7. import android.util.Log;   
  8.   
  9. import com.jftt.widget.R;   
  10.   
  11. public class CreateWidget extends Activity {   
  12.     private static final String TAG = "CreateWidget";   
  13.     int mAppWidgetId;   
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {   
  17.         super.onCreate(savedInstanceState);   
  18.         Log.i(TAG, " on WidgetConf ... ");   
  19.            
  20.         setResult(RESULT_CANCELED);   
  21.         // Find the widget id from the intent.   
  22.         Intent intent = getIntent();   
  23.         Bundle extras = intent.getExtras();   
  24.         if (extras != null) {   
  25.             mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);   
  26.         }   
  27.   
  28.         // If they gave us an intent without the widget id, just bail.   
  29.         if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {   
  30.             finish();   
  31.         }   
  32.            
  33.         // return OK   
  34.         Intent resultValue = new Intent();   
  35.         resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);   
  36.            
  37.         setResult(RESULT_OK, resultValue);   
  38.         finish();   
  39.     }   
  40. }  
  • 1
  • 2
  • 下一页

相关内容