歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之App Widget開發實例

Android之App Widget開發實例

日期:2017/3/1 10:25:55   编辑:Linux編程

前面一節已經實現了一個簡單的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、

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

下面是我們的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/widget_bg1"
  6. android:gravity="center"
  7. android:id="@+id/layout"
  8. android:orientation="vertical" >
  9. <TextView
  10. android:id="@+id/txtMonth"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:textColor="#000000"
  14. android:layout_margin="2dp"
  15. android:text="" />
  16. <TextView
  17. android:id="@+id/txtDay"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:textColor="#990033"
  21. android:textSize="25dp"
  22. android:text="" />
  23. <TextView
  24. android:id="@+id/txtWeekDay"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_margin="2dp"
  28. android:textColor="#000000"
  29. android:text="" />
  30. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved