AppWidget 如android案頭上的時鐘

來源:互聯網
上載者:User

標籤:android

一、
1、App widget 是在home案頭的一個空間,比如時鐘,播放器播放時小表徵圖可以點擊下一首等那個。

2、AppWidgetProviderInfo 對象: 為App Widget 提供中繼資料,包括布局,更新頻率等資料,這個對象被定義在xml檔案當中。

3、AppWidgetProvider : 定義一個App Widget 的基本生命週期函數。

二、建立App Widget步驟:
   1、定義AppWidgetProviderInfo : 在res/xml檔案夾中定義 example_appwidget_info.xml檔案
      android:updatePeriodMills="86400000"  定義更新的時間
   

  2.在AndroidManifest.xml加入

 

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



   3. 實現AppWidgetProvider:
      upDatate:在到達指定更新時間之後或者當使用者向案頭添加App Widget時調用。
      onDeleted:當App widget 被刪除時被調用。
      onEnabled: 當一個App widget的執行個體第一次被建立時,調用。
      onDisabled: 當最後一個Appp Widget執行個體被刪除後,會調用。
      onReveice: 接收廣播事件。

例如:

1. 在res下建立xml檔案夾,在裡面建立example_appwidget_info.xml檔案:

<span style="font-size:14px;"><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:minWidth="294dp"    android:minHeight="72dp"    android:updatePeriodMillis="86400000"    android:initialLayout="@layout/example_appwidget" ></appwidget-provider></span><pre class="java" name="code"> 

 

2. 在AndroidManifest.xm加入:

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

 

 

3.編寫代碼:

package mars.appwidget01;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;public class ExampleAppWidgetProvider extends AppWidgetProvider{@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {System.out.println("onupdate");super.onUpdate(context, appWidgetManager, appWidgetIds);}@Overridepublic void onDeleted(Context context, int[] appWidgetIds) {System.out.println("onDeleted");super.onDeleted(context, appWidgetIds);}@Overridepublic void onDisabled(Context context) {System.out.println("onDisabled");super.onDisabled(context);}@Overridepublic void onEnabled(Context context) {System.out.println("onEnabled");super.onEnabled(context);}}


 

 

 

 

AppWidget 如android案頭上的時鐘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.