自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解Android Widget筆記學(xué)習(xí)教程

移動開發(fā)
Android Widget筆記學(xué)習(xí)教程是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)Android Widget中的應(yīng)用,具體內(nèi)容的實現(xiàn)來看本文詳解。

Android Widget筆記學(xué)習(xí)教程是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)Android Widget中的應(yīng)用,具體內(nèi)容的實現(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部分

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView   
  8. android:id="@+id/tvCurrTime" 
  9.     android:layout_width="wrap_content"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/hello" 
  12.     android:textColor="@color/black" 
  13.     /> 
  14. </LinearLayout> 

2、hello_widget_provider.xml部分

  1. <?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) -->

  1. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
  2. android:minWidth="146dip" 
  3. android:minHeight="72dip" 
  4. android:initialLayout="@layout/main"> 
  5. </appwidget-provider> 

3、AndroidManifest.xml部分

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.woody.testWidget" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  7.       <!-- HelloWidgetProvider為那個class(業(yè)務(wù)處理) --> 
  8.  
  9. <receiver android:name=".HelloWidgetProvider" android:label="@string/app_name"> 
  10.     <intent-filter> 
  11.     <!-- 指定了的 --> 
  12.     <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
  13.     </intent-filter> 
  14.     <meta-data android:name="android.appwidget.provider" 
  15.  
  16. <!-- 為上面指定了的widget --> 
  17.     android:resource="@xml/hello_widget_provider" /> 
  18.    </receiver> 
  19.     </application> 
  20. </manifest> 

4、HelloWidgetProvider類的部分(進(jìn)行時間的計算,核心onUpdate方法部分)

  1. @Override  
  2. public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
  3.     int[] appWidgetIds) {  
  4.    Timer timer = new Timer();  
  5.    timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);  

5、MyTime(自己寫的定時器)

  1. public class MyTime extends TimerTask {  
  2.  
  3. RemoteViews remoteViews;  
  4. AppWidgetManager appWidgetManager;  
  5. ComponentName thisWidget;  
  6. DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,  
  7. Locale.getDefault());  
  8. public MyTime(Context context, AppWidgetManager appWidgetManager) {  
  9. this.appWidgetManager = appWidgetManager;  
  10. remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);  
  11. thisWidget = new ComponentName(context, HelloWidgetProvider.class);  
  12. }  
  13. @Override  
  14. public void run() {  
  15. remoteViews.setTextViewText(R.id.tvCurrTime,  
  16. "Time = " + format.format(new Date()));  
  17. appWidgetManager.updateAppWidget(thisWidget, remoteViews);  
  18. }  

小結(jié):詳解Android Widget筆記學(xué)習(xí)教程的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-09-07 16:28:46

QT WidgetQWidget

2011-09-07 11:15:25

2011-09-07 13:42:36

Android Wid實例

2011-09-07 13:00:36

2011-09-09 11:05:56

Widget

2011-09-09 20:14:58

Android Wid

2010-07-13 09:02:19

Widget開發(fā)

2011-09-09 16:38:51

Android Wid源碼

2011-09-07 13:18:40

Android Wid

2011-09-07 17:19:16

Web widget

2011-09-07 16:36:00

Qt Widget

2011-02-28 13:04:27

RelativeLayAndroid Wid

2011-09-08 13:11:07

Android Wid實例

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用鍵

2011-09-08 15:40:45

Android Wid組件

2011-09-07 14:25:53

Android Wid設(shè)計

2013-06-28 13:38:45

AndroidAndroidMani

2011-09-07 13:06:04

Android Wid

2010-06-03 11:12:55

Hadoop

2011-09-07 14:20:42

Android Wid組件
點贊
收藏

51CTO技術(shù)棧公眾號