Android Widget 桌面組件開發(fā)介紹
Android widget 桌面組件開發(fā)
Widget是Android1.5版所引進的特性之一.Widget,可讓用戶在主屏幕界面及時了解程序顯示的重要信息.標準的Android系統(tǒng)已包含幾個Widget的示例,如模擬時鐘,音樂播放器等.
一、AppWidget 框架類
1、AppWidgetProvider :繼承自 BroadcastRecevier , 在AppWidget 應(yīng)用 update、enable、disable 和 delete 時接收通知。其中,onUpdate、onReceive 是最常用到的方法,它們接收更新通知。
2、 AppWidgetProvderInfo:描述 AppWidget 的大小、更新頻率和初始界面等信息,以XML 文件形式存在于應(yīng)用的 res/xml/目錄下。
3、AppWidgetManger :負責管理 AppWidget ,向 AppwidgetProvider 發(fā)送通知。
4、RemoteViews :一個可以在其他應(yīng)用進程中運行的類,向 AppWidgetProvider 發(fā)送通知。
二、AppWidget 框架的主要類介紹
1) AppWidgetManger 類
bindAppWidgetId(int appWidgetId, ComponentName provider)
通過給定的ComponentName 綁定appWidgetId
getAppWidgetIds(ComponentName provider)
通過給定的ComponentName 獲取AppWidgetId
getAppWidgetInfo(int appWidgetId)
通過AppWidgetId 獲取 AppWidget 信息
getInstalledProviders()
返回一個List<AppWidgetProviderInfo>的信息
getInstance(Context context)
獲取 AppWidgetManger 實例使用的上下文對象
updateAppWidget(int[] appWidgetIds, RemoteViews views)
通過appWidgetId 對傳進來的 RemoteView 進行修改,并重新刷新AppWidget 組件
updateAppWidget(ComponentName provider, RemoteViews views)
通過 ComponentName 對傳進來的 RemoeteView 進行修改,并重新刷新AppWidget 組件
updateAppWidget(int appWidgetId, RemoteViews views)
通過appWidgetId 對傳進來的 RemoteView 進行修改,并重新刷新AppWidget 組件
2) 繼承自 AppWidgetProvider 可實現(xiàn)的方法為如下:
1、onDeleted(Context context, int[] appWidgetIds)
2、onDisabled(Context context)
3、onEnabled(Context context)
4、onReceive(Context context, Intent intent)
Tip:因為 AppWidgetProvider 是繼承自BroadcastReceiver 所以可以重寫onRecevie 方法,當然必須在后臺注冊Receiver
5、onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
三,Demo 詳解
1.建立Widget內(nèi)容提供者文件,我們在res下建立xml文件夾,并且新建一個widget_provider.xml代碼入下:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="50dip" android:minHeight="50dip" android:updatePeriodMillis="10000" android:initialLayout="@layout/main" />
Tip:上文說過AppWidgetProvderInfo 是在res/xml 的文件形式存在的,看參數(shù)不難理解,比較重要的是這里android:initialLayout="@layout/main" 此句為指定桌面組件的布局文件。
主要設(shè)置的參數(shù)如下:
minWidth: 定義Wdiget組件的寬度
minHeight: 定義Wdiget組件的高度
updatePeriodMillis: 更新的時間周期
initialLayout: Widget的布局文件
configure: 如果需要在啟動前先啟動一個Activity進行設(shè)置,在這里給出Activity的完整類名(后面會說到,與一般Activity的實現(xiàn)有些許差別)
*Widget大小的計算 :(單元格數(shù)*74)-2,API上說是為了防止像素計算時的整數(shù)舍入導(dǎo)致錯所以-2...不是很明白
2.修改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" android:background="@drawable/wordcup" > <TextView android:id="@+id/wordcup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="12px" android:textColor="#ff0000" /> </LinearLayout>
Tips:定義了Widget界面布局的XML文件(位于res/layout/..),需要注意的是使用的組件必須是RemoteViews所支持的,目前原生API中支持的組件如下:
FrameLayout、LinearLayout、RelativeLayout
AnalogClock、Button、Chronmeter、ImageButton、ImageView、ProgressBar、TextView
*如果使用了除此之外的組件,則在Widget創(chuàng)建時會導(dǎo)致android.view.InflateExceptionn異常。
PS:這就導(dǎo)致有一些功能或樣式無法實現(xiàn),如很基本的list或文本編輯框都是無法直接實現(xiàn)的。如果想自定義Widget中的View的話只能通過修改framework來提供相應(yīng)組件的支持。
3.寫一個類繼承自AppWidgetProvider
package com.android.tutor; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Timer; import java.util.TimerTask; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.widget.RemoteViews; public class WidetDemo extends AppWidgetProvider { /** Called when the activity is first created. */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Timer timer = new Timer(); timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, 60000); super.onUpdate(context, appWidgetManager, appWidgetIds); } private class MyTime extends TimerTask{ RemoteViews remoteViews; AppWidgetManager appWidgetManager; ComponentName thisWidget; public MyTime(Context context,AppWidgetManager appWidgetManager){ this.appWidgetManager = appWidgetManager; remoteViews = new RemoteViews(context.getPackageName(),R.layout.main); thisWidget = new ComponentName(context,WidetDemo.class); } public void run() { Date date = new Date(); Calendar calendar = new GregorianCalendar(2010,06,11); long days = (((calendar.getTimeInMillis()-date.getTime())/1000))/86400; remoteViews.setTextViewText(R.id.wordcup, "距離南非世界杯還有" + days+"天"); appWidgetManager.updateAppWidget(thisWidget, remoteViews); } } }
AppWidgetProvider實際上就是一個BroadcastReceiver,里面提供了以下函數(shù):
onReceive(Context, Intent)
onUpdate(Context , AppWidgetManager, int[] appWidgetIds)
onEnabled(Context)
onDeleted(Context, int[] appWidgetIds)
onDisabled(Context)
可通過重寫以上函數(shù)來監(jiān)聽Widget狀態(tài)的變化并進行相應(yīng)的處理。
onUpdate 為組件在桌面上生成時調(diào)用,并更新組件UI,onReceiver 為接收廣播時調(diào)用更新UI,一般這兩個方法是比較常用的。
Widget的更新與Activity不同,必須借助于RemoteViews和AppWidgetMananger。
函數(shù)調(diào)用周期 [啟動 - 無confiure Activity] onReceive onEnabled —— 第一個widget被顯示 onReceive onUpdate —— 刷新界面 [啟動 - 帶confiuration Activity] onReceive onUpdate [拖動] <無狀態(tài)變化> [周期更新] onReceive onUpdate [刪除] onReceive onDeleted —— widget被刪除 onReceive onDisabled —— 最后一個widget被移除 [啟動時位置不夠] onReceive onEnabled onReceive onUpdate onReceive onDeleted onReceive onDisabled
*每次狀態(tài)的變化會觸發(fā)onReceive,一般該函數(shù)是不需要重寫的。
四、修改配置文件AndroidManifest.xml,后臺注冊Receiver,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.tutor" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name=".WidetDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" /> </receiver> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>
Tips:
因為是桌面組件,所以暫時不考慮使用Activity 界面,當然你在實現(xiàn)做項目時可能會需要點擊時跳轉(zhuǎn)到Activity 應(yīng)用程序上做操作,典型的案例為Android 提供的音樂播放器。
上面代碼中比較重要的是這一句 <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_provider"></meta-data> 大意為指定桌面應(yīng)用程序的AppWidgetProvderInfo 文件,使其可作其管理文件。
五、添加修改資源
增加圖片素材。
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, WidetDemo!</string> <string name="app_name">DaysToWorldCup</string> </resources>
以上就是對Android widget 資料的整理,希望能幫助需要的朋友。
相關(guān)文章
Android開發(fā)之React Navigation 導(dǎo)航欄樣式調(diào)整+底部角標消息提示
這篇文章主要介紹了React Navigation 導(dǎo)航欄樣式調(diào)整+底部角標消息提示的相關(guān)知識,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05Android RippleDrawable 水波紋/漣漪效果的實現(xiàn)
這篇文章主要介紹了Android RippleDrawable 水波紋/漣漪效果的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08