1. 程式人生 > >android之App Widget開發例項

android之App Widget開發例項

       前面一節已經實現了一個簡單的App Widget,這裡將通過一個例項繼續深入學習App Widget。

       首先繼續瞭解下App Widget框架的主要的類:

       AppWidgetProvider:繼承自BroadcastReceiver,在App Widget應用update,enable,disable和deleted時接受通知。其中onUpdate,onReceive是最常用到的方法。

      AppWidgetProviderInfo:描述AppWidget的大小,更新頻率和初始介面等資訊,以xml檔案的形式存在於應用中的res/xml目錄下。

     AppWidgetManager:負責管理AppWidget,向AppWidgetProvider傳送通知。

     RemoteViews:一個可以在其他應用程序中執行的類,是構造AppWidget的核心。

     下面開始程式碼的編寫,首先在res/xml下建立myappwidetprovider.xml、

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="100dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/myappwidget"
    >
</appwidget-provider>
  上面分別是 定義widget的寬度,高度,更新週期,以及layout的widget佈局。

  下面是我們的佈局檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/widget_bg1"
    android:gravity="center"
    android:id="@+id/layout"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtMonth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_margin="2dp"
        android:text="" />
<TextView
        android:id="@+id/txtDay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#990033"
        android:textSize="25dp"
        android:text="" />
<TextView
        android:id="@+id/txtWeekDay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:textColor="#000000"
        android:text="" />
</LinearLayout>

   對應佈局widget要求比較高,大家自行設計,更加美觀的介面。

  接下來是我們的核心程式碼ExampleAppWidgetProvider類了:

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.text.format.Time;
import android.widget.RemoteViews;
import android.widget.Toast;

public class ExampleAppWidgetProvider extends AppWidgetProvider{
    private String[] months={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
    private String[] days={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		
		RemoteViews remoteViews=new RemoteViews(context.getPackageName(), R.layout.myappwidget);
		Time time=new Time();
		time.setToNow();
		String month=time.year+" "+months[time.month];
		remoteViews.setTextViewText(R.id.txtDay, new Integer(time.monthDay).toString());
		remoteViews.setTextViewText(R.id.txtMonth, month);
		remoteViews.setTextViewText(R.id.txtWeekDay, days[time.weekDay]);
		Intent intent=new Intent("cn.com.karl.widget.click");
		PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent);
		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
		
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
		if(intent.getAction().equals("cn.com.karl.widget.click")){
			Toast.makeText(context, "點選了widget日曆", 1).show();
		}
	}
}

   上面程式碼忘記做註釋了,在這類分別解釋下,使用remoteViews類分別載入上來佈局檔案的相應ID設定好值,然後PendingIntent 這就沒什麼好解釋的了。

  最後在manifest中加入:

 <receiver android:name="ExampleAppWidgetProvider" >
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="cn.com.karl.widget.click" >
                </action>
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/myappwidetprovider" />
        </receiver>

   這樣就完成了,執行專案看一下載手機上執行的效果吧:

  

   上面就是我們自己定義的AppWidget顯示效果,點選它:

   這裡為了表示點選了它,使用了Toast列印資訊,當然我們也可以點選它之後啟動相應的Activity。