歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Widget簡單應用之奧運會倒計時

Android Widget簡單應用之奧運會倒計時

日期:2017/3/1 10:23:12   编辑:Linux編程

Android Widget桌面小部件是可以在主頁上顯示並頻繁更新的視圖。作為視圖,部件的觀感通過布局xml文件來定義。對於部件,除了視圖的布局,還需要定義部件視圖將需要在屏幕上占用多大空間。

部件視圖還包括一對Java類,他們負責初始化視圖並頻繁更新它,這些Java類負責在主屏幕上管理部件的生命周期。當將部件拖到屏幕上,以及將部件拖到回收站進行卸載時,這些類進行相應。

下面通過一個倫敦奧運會倒計時的簡單Widget例子來說明如何創建一個桌面小部件。
1、定義一個AppWidgetProviderInfo,在/res/xml/widget.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:minWidth="50dip"
  5. android:minHeight="50dip"
  6. android:updatePeriodMillis="10000"
  7. android:initialLayout="@layout/main"
  8. >
  9. </appwidget-provider>
2、為AppWidget指定樣式和布局: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. android:background="@drawable/icon"
  7. >
  8. <TextView
  9. android:id="@+id/Olympic"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="@string/hello"
  13. android:textSize="12px"
  14. android:textColor="#ff00ff"
  15. />
  16. </LinearLayout>
3、實現AppWidgetProvider類
  1. public class WidgetDemo extends AppWidgetProvider {
  2. @Override
  3. public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  4. int[] appWidgetIds) {
  5. // TODO Auto-generated method stub
  6. Timer timer = new Timer();
  7. timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, 60000);
  8. super.onUpdate(context, appWidgetManager, appWidgetIds);
  9. }
  10. //定時任務類
  11. private class MyTime extends TimerTask{
  12. RemoteViews remoteViews;
  13. AppWidgetManager appWidgetManager;
  14. ComponentName thisWidget;
  15. public MyTime(Context context,AppWidgetManager appWidgetManager){
  16. this.appWidgetManager = appWidgetManager;
  17. remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);
  18. thisWidget = new ComponentName(context,WidgetDemo.class);
  19. }
  20. //定時任務內容
  21. public void run() {
  22. Date date = new Date();
  23. Calendar calendar = new GregorianCalendar(2012,06,28);
  24. long days = ((calendar.getTimeInMillis()-date.getTime())/(1000*60*60*24));
  25. remoteViews.setTextViewText(R.id.Olympic, "距離倫敦奧運會還有" + days+"天");
  26. //更新Widget內容
  27. appWidgetManager.updateAppWidget(thisWidget, remoteViews);
  28. }
  29. }
  30. }
4、在AndroidManifest.xml中注冊:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.test.widget"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <receiver android:name=".WidgetDemo"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  11. </intent-filter>
  12. <meta-data android:name="android.appwidget.provider"
  13. android:resource="@xml/widget"
  14. />
  15. </receiver>
  16. </application>
  17. </manifest>

運行程序,長按桌面在談出的選項中選擇窗口小部件,結果如圖:



然後選擇olympic widget,結果如圖所示:

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved