首先記錄下基本的步驟吧
(1)總的來說就是修改三個XML,一個class...
(2)第一個xml是布局XML檔案(如:main.xml),是這個widget的。一般來說如果用這個組件顯示時間,那就只在這個布局XML中聲明一個textview就OK了
(3)第二個xml是widget_provider.xml,主要是用於聲明一個appwidget的 (其中:updatePeriodMillis是定時更新時間、每秒都會調用該 appwidget的onUpdate方法的作用、Layout就是指定上面那個main.xml)
(4)第三個是AndroidManifest.xml中,註冊broadcastReceiver資訊
(5)最後那個class用於做一些商務邏輯操作
具體的一些代碼
1、main.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:id="@+id/tvCurrTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@color/black"
/>
</LinearLayout>
2、hello_widget_provider.xml部分
<?xml version="1.0" encoding="utf-8"?>
<!-- appwidget的updatePeriodMillis:定時更新時間、意思是每秒都會調用該 appwidget的onUpdate方法,onUpdate方法在兩種情況下被調用,第一種是添加appwidget時,第二種是每一個更新周期結束時調用一次onUpdate方法。註:此屬性在SDK1.5版本以後就失效了!!!如果需要更新的話必須自己寫個定時器哈,我的版本為2.2) -->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:initialLayout="@layout/main">
</appwidget-provider>
3、AndroidManifest.xml部分
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.woody.testWidget"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- HelloWidgetProvider為那個class(業務處理) -->
<receiver android:name=".HelloWidgetProvider" android:label="@string/app_name">
<intent-filter>
<!-- 指定了的 -->
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
<!-- 為上面指定了的widget -->
android:resource="@xml/hello_widget_provider" />
</receiver>
</application>
</manifest>
4、HelloWidgetProvider類的部分(進行時間的計算,核心onUpdate方法部分)
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
}
5、MyTime(自己寫的定時器)
public class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,
Locale.getDefault());
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, HelloWidgetProvider.class);
}
@Override
public void run() {
remoteViews.setTextViewText(R.id.tvCurrTime,
"Time = " + format.format(new Date()));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
為了您的安全,請只開啟來源可靠的網址
開啟網站 取消
來自: http://hi.baidu.com/autumn%C0%B6%B8%F1%D7%D3/blog/item/f4ae54f5b1a94bff7609d746.html 從零開始建立一個Android主畫面Widgethttp://doc.chinaunix.net/android/200912/259645.shtml