Android學習筆記————為應用添加AppWidget

來源:互聯網
上載者:User

本文來自http://blog.csdn.net/chenshaoyang0011 轉載請申明文章出處!

Android通知系統是它的一大特色,而其中,AppWidget是其中一個亮點。在開發應用的中,很多時候可以為其添加一個AppWidget顯示在案頭中,及時方便的與使用者進行

互動。這裡就簡單的熟悉一下開發一個AppWidget的流程吧。

想要在應用中建立一個AppWidget,至少需要以下幾樣東西:

1、需要建立一個AppWidgetProviderInfo,來描述AppWidget的中繼資料。2、需要實現一個自己的AppWidgetProvider對AppWidget進行更新等操作。2、需要布局檔案來

描述AppWidget的布局。

那麼,下面就開始建立一個AppWidget吧。

一、在AndroidManifest.xml中聲明一個AppWidget

首先我們需要在AndroidManifest.xml中聲明AppWidgetProvider。格式如下:

[java]
view plaincopyprint?
  1. <receiver android:name="MyAppWidgetProvider" >  
  2.     <intent-filter>  
  3.         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
  4.     </intent-filter>  
  5.     <meta-data android:name="android.appwidget.provider"  
  6.                android:resource="@xml/my_appwidget_info" />  
  7. </receiver>  
<receiver android:name="MyAppWidgetProvider" >    <intent-filter>        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />    </intent-filter>    <meta-data android:name="android.appwidget.provider"               android:resource="@xml/my_appwidget_info" /></receiver>

可以看出AppWidgetProvider實際上就是一個BroadcastReceiver,它接收特定的Broadcast。<meta-data>標籤描述了AppWidget所使用的中繼資料,android:resource則聲明了定義中繼資料的xml檔案的位置。

二、添加AppWidgetProviderInfo中繼資料

AppWidgetProviderInfo描述了AppWidget的本質特性,例如,AppWidget更新的周期,最小的寬度、長度,所使用的布局檔案是什麼,以及添加AppWidget需要啟動的

configuration Activity等。我們需要在XML中來定義AppWidgetProviderInfo對象,這個XML檔案應該儲存在res/xml檔案夾下。下面是一個範例:

[java]
view plaincopyprint?
  1. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:minWidth="294dp"  
  3.     android:minHeight="72dp"  
  4.     android:updatePeriodMillis="86400000"  
  5.     android:previewImage="@drawable/preview"  
  6.     android:initialLayout="@layout/example_appwidget"  
  7.     android:configure="com.example.android.MyAppWidgetConfigure"   
  8.     android:resizeMode="horizontal|vertical">  
  9. </appwidget-provider>  
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:minWidth="294dp"    android:minHeight="72dp"    android:updatePeriodMillis="86400000"    android:previewImage="@drawable/preview"    android:initialLayout="@layout/example_appwidget"    android:configure="com.example.android.MyAppWidgetConfigure"     android:resizeMode="horizontal|vertical"></appwidget-provider>

<appwidget-provider>需要使用這個標籤來定義AppWidgetProviderInfo。下面對範例中使用到的屬性做下說明。

minWidthminHeight定義了AppWidget需要佔據的最小的空間。

updatePeriodMillis定義了大概多久AppWidget需要更新一次,這裡定義的只是一個大概的時間,系統不能做出精確的保證。

previewImage定義了在使用者選擇AppWidget時做現實的表徵圖。

initialLayout定義了AppWidget所使用的布局檔案。

configure定義了AppWidget在添加的時候需要啟動的configuration Activity 用於執行配置的工作。

resizeMode定義了縮放模式。

三、建立AppWidget所使用的布局檔案

在建立AppWidget時必須建立一個布局檔案,為其提供布局描述。AppWidget建立視圖時,需要根據RemoteViews來建立。而出於效率等因素的考慮,很多控制項在

RemoteViews中是被支援的。以下列出能在RemoteViews中使用的UI控制項:

layout : FrameLayout , LinearLayout , RelativeLayout

widget : AnalogClock , Button , Chronometer , ImageButton , ImageView , ProgressBar , TextView , ViewFlipper , ListView , GridView
, StackView , AdapterViewFlipper

四、建立一個AppWidgetProvider的子類

前面提到過AppWidgetProvider就是一個BroadcastReceiver。對,它其實確實是繼承自BroadcastReceiver,只是它為了更加方便的處理AppWidget的廣播進行了封裝。

AppWidgetProvider在接收到AppWidget的廣播的時候,會根據類型分別觸發以下幾個方法:

onUpdate() : 當AppWidget需要更新時,會觸發這個方法,我們需要重寫這個方法,在裡面實現更新的操作。如果沒有定義configuration Activity,那麼在添加一個AppWidget

時,也會觸發此方法。

onDelete(Context , int[] ):當AppWidget從AppWidgetHost中刪除時,會觸發此方法。

onEnabled(Context ):如果為一個應用添加了多個AppWidget,只有在第一個AppWidget被添加時,此方法才會被調用。

onDisabled(Context ):當一個應用的最後一個AppWidget從AppWidgetHost中刪除時,會觸發此方法。

onReceive(Context , Intent ):這實際上就是BroadcastReceiver中的方法,當任何一個Broadcast被接收到時,會調用此方法,並且會在以上回調方法之前被調用。

五、建立一個ConfigurationActivity(可選)

如果需要AppWidget添加的時候做一些配置工作,就可以使用Configuration Activity。要使用ConfigurationActivity首先需要像普通的Activity一樣在AndroidManifest.xml中

進行聲明:

[java]
view plaincopyprint?
  1. <activity android:name=".ExampleAppWidgetConfigure">  
  2.     <intent-filter>  
  3.         <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>  
  4.     </intent-filter>  
  5. </activity>  
<activity android:name=".ExampleAppWidgetConfigure">    <intent-filter>        <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>    </intent-filter></activity>

只是這裡需要添加action類型為android.appwidget.action.APPWIDGET_CONFIGURE的intent-filter。然後,需要在AppWidgetProviderInfo中進行聲明:

[java]
view plaincopyprint?
  1. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     ...  
  3.     android:configure="com.example.android.ExampleAppWidgetConfigure"   
  4.     ... >  
  5. </appwidget-provider>  
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    ...    android:configure="com.example.android.ExampleAppWidgetConfigure"     ... ></appwidget-provider>

最後,當然是需要建立Activity了,在Configuration Activity中,需要執行一些必要的操作:

1、擷取AppWidget ID

[java]
view plaincopyprint?
  1. Intent intent = getIntent();  
  2. Bundle extras = intent.getExtras();  
  3. if (extras != null) {  
  4.     mAppWidgetId = extras.getInt(  
  5.             AppWidgetManager.EXTRA_APPWIDGET_ID,   
  6.             AppWidgetManager.INVALID_APPWIDGET_ID);  
  7. }  
Intent intent = getIntent();Bundle extras = intent.getExtras();if (extras != null) {    mAppWidgetId = extras.getInt(            AppWidgetManager.EXTRA_APPWIDGET_ID,             AppWidgetManager.INVALID_APPWIDGET_ID);}

2、進行必要的配置操作。

3、擷取AppWidgetManager執行個體、更新RemoteViews

[java]
view plaincopyprint?
  1. AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);  
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
[java]
view plaincopyprint?
  1. RemoteViews views = new RemoteViews(context.getPackageName(),  
  2. R.layout.example_appwidget);  
  3. appWidgetManager.updateAppWidget(mAppWidgetId, views);  
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.example_appwidget);appWidgetManager.updateAppWidget(mAppWidgetId, views);

4、設定Activity result,並且返回一個Intent。

[java]
view plaincopyprint?
  1. Intent resultValue = new Intent();  
  2. resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);  
  3. setResult(RESULT_OK, resultValue);  
  4. finish();  
Intent resultValue = new Intent();resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);setResult(RESULT_OK, resultValue);finish();

這樣一個就建立好了一個Configuration Activity了。

執行完上面的步驟,就已經建立了一個可以在案頭進行顯示的AppWidget了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.