Android開(kāi)發(fā)之AppWidget詳解
Android通知系統(tǒng)是它的一大特色,而其中,AppWidget是其中一個(gè)亮點(diǎn)。在開(kāi)發(fā)應(yīng)用的中,很多時(shí)候可以為其添加一個(gè)AppWidget顯示在桌面中,及時(shí)方便的與用戶(hù)進(jìn)行
交互。這里就簡(jiǎn)單的熟悉一下開(kāi)發(fā)一個(gè)AppWidget的流程吧。
想要在應(yīng)用中創(chuàng)建一個(gè)AppWidget,至少需要以下幾樣?xùn)|西:
- 需要?jiǎng)?chuàng)建一個(gè)AppWidgetProviderInfo,來(lái)描述AppWidget的元數(shù)據(jù)。
- 需要實(shí)現(xiàn)一個(gè)自己的AppWidgetProvider對(duì)AppWidget進(jìn)行更新等操作。
- 需要布局文件來(lái)描述AppWidget的布局。
那么,下面就開(kāi)始創(chuàng)建一個(gè)AppWidget吧。
一、在AndroidManifest.xml中聲明一個(gè)AppWidget
首先我們需要在AndroidManifest.xml中聲明AppWidgetProvider。格式如下:
<receiver android:name="MyAppWidgetProvider" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/my_appwidget_info" /> </receiver>
可以看出AppWidgetProvider實(shí)際上就是一個(gè)BroadcastReceiver,它接收特定的Broadcast。<meta-data>標(biāo)簽描述了AppWidget所使用的元數(shù)據(jù),android:resource則聲明了定義元數(shù)據(jù)的xml文件的位置。
二、添加AppWidgetProviderInfo元數(shù)據(jù)
AppWidgetProviderInfo描述了AppWidget的本質(zhì)特性,例如,AppWidget更新的周期,最小的寬度、長(zhǎng)度,所使用的布局文件是什么,以及添加AppWidget需要啟動(dòng)的
configuration Activity等。我們需要在XML中來(lái)定義AppWidgetProviderInfo對(duì)象,這個(gè)XML文件應(yīng)該保存在res/xml文件夾下。下面是一個(gè)范例:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis="86400000" android:previewImage="@drawable/preview" android:initialLayout="@layout/example_appwidget" android:configure="com.example.android.MyAppWidgetConfigure" android:resizeMode="horizontal|vertical"> </appwidget-provider>
<appwidget-provider>需要使用這個(gè)標(biāo)簽來(lái)定義AppWidgetProviderInfo。下面對(duì)范例中使用到的屬性做下說(shuō)明。
minWidth、minHeight定義了AppWidget需要占據(jù)的最小的空間。
updatePeriodMillis定義了大概多久AppWidget需要更新一次,這里定義的只是一個(gè)大概的時(shí)間,系統(tǒng)不能做出精確的保證。
previewImage定義了在用戶(hù)選擇AppWidget時(shí)做現(xiàn)實(shí)的圖標(biāo)。
initialLayout定義了AppWidget所使用的布局文件。
configure定義了AppWidget在添加的時(shí)候需要啟動(dòng)的configuration Activity 用于執(zhí)行配置的工作。
resizeMode定義了縮放模式。
三、創(chuàng)建AppWidget所使用的布局文件
在創(chuàng)建AppWidget時(shí)必須創(chuàng)建一個(gè)布局文件,為其提供布局描述。AppWidget創(chuàng)建視圖時(shí),需要根據(jù)RemoteViews來(lái)創(chuàng)建。而出于效率等因素的考慮,很多控件在
RemoteViews中是被支持的。以下列出能在RemoteViews中使用的UI控件:
layout : FrameLayout , LinearLayout , RelativeLayout
widget : AnalogClock , Button , Chronometer , ImageButton , ImageView , ProgressBar , TextView , ViewFlipper , ListView , GridView , StackView , AdapterViewFlipper
四、創(chuàng)建一個(gè)AppWidgetProvider的子類(lèi)
前面提到過(guò)AppWidgetProvider就是一個(gè)BroadcastReceiver。對(duì),它其實(shí)確實(shí)是繼承自BroadcastReceiver,只是它為了更加方便的處理AppWidget的廣播進(jìn)行了封裝。
AppWidgetProvider在接收到AppWidget的廣播的時(shí)候,會(huì)根據(jù)類(lèi)型分別觸發(fā)以下幾個(gè)方法:
onUpdate() : 當(dāng)AppWidget需要更新時(shí),會(huì)觸發(fā)這個(gè)方法,我們需要重寫(xiě)這個(gè)方法,在里面實(shí)現(xiàn)更新的操作。如果沒(méi)有定義configuration Activity,那么在添加一個(gè)AppWidget
時(shí),也會(huì)觸發(fā)此方法。
onDelete(Context , int[] ):當(dāng)AppWidget從AppWidgetHost中刪除時(shí),會(huì)觸發(fā)此方法。
onEnabled(Context ):如果為一個(gè)應(yīng)用添加了多個(gè)AppWidget,只有在第一個(gè)AppWidget被添加時(shí),此方法才會(huì)被調(diào)用。
onDisabled(Context ):當(dāng)一個(gè)應(yīng)用的最后一個(gè)AppWidget從AppWidgetHost中刪除時(shí),會(huì)觸發(fā)此方法。
onReceive(Context , Intent ):這實(shí)際上就是BroadcastReceiver中的方法,當(dāng)任何一個(gè)Broadcast被接收到時(shí),會(huì)調(diào)用此方法,并且會(huì)在以上回調(diào)方法之前被調(diào)用。
五、創(chuàng)建一個(gè)ConfigurationActivity(可選)
如果需要AppWidget添加的時(shí)候做一些配置工作,就可以使用Configuration Activity。要使用ConfigurationActivity首先需要像普通的Activity一樣在AndroidManifest.xml中
進(jìn)行聲明:
<activity android:name=".ExampleAppWidgetConfigure"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/> </intent-filter> </activity>
只是這里需要添加action類(lèi)型為android.appwidget.action.APPWIDGET_CONFIGURE的intent-filter。然后,需要在AppWidgetProviderInfo中進(jìn)行聲明:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" ... android:configure="com.example.android.ExampleAppWidgetConfigure" ... > </appwidget-provider>
最后,當(dāng)然是需要?jiǎng)?chuàng)建Activity了,在Configuration Activity中,需要執(zhí)行一些必要的操作:
1、獲取AppWidget ID
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
2、進(jìn)行必要的配置操作,獲取AppWidgetManager實(shí)例、更新RemoteViews
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget); appWidgetManager.updateAppWidget(mAppWidgetId, views);
3、設(shè)置Activity result,并且返回一個(gè)Intent。
Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); setResult(RESULT_OK, resultValue); finish();
這樣一個(gè)就創(chuàng)建好了一個(gè)Configuration Activity了。
注意android8.0以后無(wú)法收到發(fā)給自己的AppWidgetProvider,需要添加
intent.setComponent(new ComponentName(context,CacheProvider.class));
Intent intent = new Intent();
intent.setAction(ACTION_CACHE_CLEAN);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setComponent(new ComponentName(context,CacheProvider.class));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.tv_clean, pendingIntent);
執(zhí)行完上面的步驟,就已經(jīng)創(chuàng)建了一個(gè)可以在桌面進(jìn)行顯示的AppWidget了。
以上就是Android開(kāi)發(fā)之AppWidget詳解的詳細(xì)內(nèi)容,更多關(guān)于Android AppWidget詳解的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能示例
- Android UI組件AppWidget控件入門(mén)詳解
- Android Widget 桌面組件開(kāi)發(fā)介紹
- Android桌面組件App Widget完整案例
- Android桌面組件App Widget用法入門(mén)教程
- Android控件AppWidgetProvider使用方法詳解
- Android TabWidget底部顯示效果
- Android小掛件(APP Widgets)設(shè)計(jì)指導(dǎo)
- 在Android中創(chuàng)建widge組件的步驟
相關(guān)文章
Android實(shí)現(xiàn)水波紋點(diǎn)擊效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)水波紋點(diǎn)擊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
基于RxJava框架實(shí)現(xiàn)獲取驗(yàn)證碼的輔助類(lèi)
這篇文章主要為大家詳細(xì)介紹了基于RxJava框架實(shí)現(xiàn)獲取驗(yàn)證碼的輔助類(lèi),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android 開(kāi)發(fā)隨手筆記之使用攝像頭拍照
在Android中,使用攝像頭拍照一般有兩種方法, 一種是調(diào)用系統(tǒng)自帶的Camera,另一種是自己寫(xiě)一個(gè)攝像的界面,本篇文章給大家介紹android開(kāi)發(fā)隨手筆記之使用攝像頭拍照,感興趣的朋友一起學(xué)習(xí)吧2015-11-11
Flutter 如何設(shè)置App的主色調(diào)與字體
App 開(kāi)發(fā)過(guò)程中,肯定希望給用戶(hù)帶來(lái)一致的體驗(yàn),這其中最基礎(chǔ)的就是色調(diào)、字體保持一致。在 Flutter 中,可以設(shè)置全局的主題色調(diào)和字體,從而在其他頁(yè)面引用主色調(diào)和字體,實(shí)現(xiàn)頁(yè)面展示層面的一致。2021-05-05
Android用 Mob 實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼實(shí)例
這篇文章主要介紹了Android用 Mob 實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android自定義實(shí)現(xiàn)循環(huán)滾輪控件WheelView
滾輪布局WheelView大家經(jīng)常使用,比如在選擇生日的時(shí)候,風(fēng)格類(lèi)似系統(tǒng)提供的DatePickerDialog,這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)循環(huán)滾輪控件WheelView,感興趣的小伙伴們可以參考一下2016-07-07
Android實(shí)現(xiàn)為L(zhǎng)istView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景
這篇文章主要介紹了Android實(shí)現(xiàn)為L(zhǎng)istView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景,以實(shí)例形式較為詳細(xì)的分析了界面元素與功能的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02

