Android widget開發由淺入深含Demo

來源:互聯網
上載者:User

一、最簡單的Widget樣本

widget程式由以下四部分組成:

a. AppWidgetProvider 的實現

b. widget外觀布局定義檔案

c. 新增widget時的配置Activity的實現(可選)

d. widget 參數設定檔

A、AppWidgetProvider 的實現:

package com.lizhen.widget;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;import android.content.Intent;import android.os.Bundle;public class DefineAppWidgetProvider extends AppWidgetProvider{//布置一個新的widget時調用@Overridepublic void onAppWidgetOptionsChanged(Context context,AppWidgetManager appWidgetManager, int appWidgetId,Bundle newOptions) {super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,newOptions);}//當案頭組件刪除時調用@Overridepublic void onDeleted(Context context, int[] appWidgetIds) {super.onDeleted(context, appWidgetIds);}//提供的組後一個組件刪除時調用@Overridepublic void onDisabled(Context context) {super.onDisabled(context);}//提供的第一個組件建立時調用@Overridepublic void onEnabled(Context context) {super.onEnabled(context);}//為各種各樣的AppWidgetProvider發送訊息@Overridepublic void onReceive(Context context, Intent intent) {super.onReceive(context, intent);}//周期更新時調用@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {super.onUpdate(context, appWidgetManager, appWidgetIds);}}

 

B、widget外觀布局定義檔案在layout中widget.xml(只做示範,當然可以任意定義)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical" >    <ImageView         android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/dog_widget"/><TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/dog" /></LinearLayout>

C、配置的這個Activity的作用是:在widget啟動前先啟動這個Activity,一般用於配置widget的資訊吧,比如顏色、大小等等!這先不用寫。

D、編寫一個widget參數設定檔,將布局、配置Activity關聯起來。在res下建立目錄xml,在xml目錄下新增檔案 widget.xml:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"      android:minWidth="72dp" android:minHeight="72dp"      android:updatePeriodMillis="86400000" android:initialLayout="@layout/widget"     >  </appwidget-provider>  

minWidth:組件的最小寬度;

minHeight:組件的最小高度;

updatePeriodMillis:自動更新的時間間隔;

initialLayout:Widg額頭的介面描述檔案;

configure:是可選的,關聯Acitvity;

 

E、在Manifest中註冊widget、BroadReceiver、Activity等資訊

<receiver            android:label="@string/app_name"            android:name="com.lizhen.widget.DefineAppWidgetProvider" >           <intent-filter ><action android:name="android.appwidget.action.APPWIDGET_UPDATE" /></intent-filter>           <meta-data android:name="android.appwidget.provider"               android:resource="@xml/widget"/>        </receiver>

好了,這樣我們完成了最最最基本的Widget程式!:

聯繫我們

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