詳解Android Widget筆記學(xué)習(xí)教程
Android Widget筆記學(xué)習(xí)教程是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)Android Widget中的應(yīng)用,具體內(nèi)容的實(shí)現(xiàn)來看本文詳解。學(xué)了那么久居然今天才曉得widget.我太慚愧了。
首先記錄下基本的步驟吧
(1)總的來說就是修改三個XML,一個class...
(2)***個xml是布局XML文件(如:main.xml),是這個widget的。一般來說如果用這個部件顯示時間,那就只在這個布局XML中聲明一個textview就OK了
(3)第二個xml是widget_provider.xml,主要是用于聲明一個appwidget的 (其中:updatePeriodMillis是定時更新時間、每秒都會調(diào)用該 appwidget的onUpdate方法的作用、Layout就是指定上面那個main.xml)
(4)第三個是AndroidManifest.xml中,注冊broadcastReceiver信息
(5)***那個class用于做一些業(yè)務(wù)邏輯操作
具體的一些代碼
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:定時更新時間、意思是每秒都會調(diào)用該 appwidget的onUpdate方法,onUpdate方法在兩種情況下被調(diào)用,***種是添加appwidget時,第二種是每一個更新周期結(jié)束時調(diào)用一次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(業(yè)務(wù)處理) -->
 - <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類的部分(進(jìn)行時間的計(jì)算,核心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);
 - }
 - }
 
小結(jié):詳解Android Widget筆記學(xué)習(xí)教程的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!















 
 
 
 
 
 
 