android入門之App Widget(一)

來源:互聯網
上載者:User

 1、AppWidgetProviderInfo
為 App widget提供中繼資料,包括布局,更新頻率等資料。這個對象被定義在xml檔案當中。
2、AppWidgetProvider
定義App widget的基本生命週期

建立一個App widget得步驟:
1、定義AppWidgetProviderInfo:
在res/xml檔案夾當中定義一個名為example_appwidget_info.xml的檔案:
<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>
2、為App widget指定樣式和布局
定義一個新的布局檔案example_appwidget.xml,在本例當中只是簡單定義一個TextView:
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="hello"
    android:id="@+id/widgetTextld"
    />

3、繼承AppWidgetProvider
onUpdate:在到達指定的更新時間之後或者當使用者案頭添加App widget時會調用該方法
onDelete:當App widget被刪除時,會調用該方法
onEnabled:當一個App widget得執行個體第一次被建立時,會調用該方法
onDisabled:當最後一個App widget執行個體被刪除後,會調用該方法
onReveice:接收廣播事件

 

例子:

在res/layout目錄下添加example_appwidget.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="firstWidgetText"
    android:id="@+id/widgetTextld"
    android:background="#0000FF"
    />
</LinearLayout>

在res/xml目錄下添加example_appwidget_info.xml

<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>

在androidManifest.xml檔案application節點之間添加如下代碼:

<receiver android:name=".AppWidgetProviderTest">
   <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"></meta-data>
  </receiver>

好了,設定檔就到此結束了,下面寫java代碼:

建立一個AppWidgetProviderTest類繼承AppWidgetProvider類,內容如下:

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;

public class AppWidgetProviderTest extends AppWidgetProvider {

 //接收廣播事件
 @Override
 public void onReceive(Context context, Intent intent) {
  System.out.println("onReceive");
  super.onReceive(context, intent);
 }

 //在到達指定的更新時間之後或者當使用者案頭添加App widget時會調用該方法
 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager,
   int[] appWidgetIds) {
  System.out.println("onUpdate");
  super.onUpdate(context, appWidgetManager, appWidgetIds);
 }

 //當App widget被刪除時,會調用該方法
 @Override
 public void onDeleted(Context context, int[] appWidgetIds) {
  System.out.println("onDeleted");
  super.onDeleted(context, appWidgetIds);
 }

 //當一個App widget得執行個體第一次被建立時,會調用該方法
 @Override
 public void onEnabled(Context context) {
  System.out.println("onEnabled");
  super.onEnabled(context);
 }

 //當最後一個App widget執行個體被刪除後,會調用該方法
 @Override
 public void onDisabled(Context context) {
  System.out.println("onDisabled");
  super.onDisabled(context);
 }
}

 

相關文章

聯繫我們

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