簡單掌握Android Widget桌面小部件的創(chuàng)建步驟
一、Widget設(shè)計步驟
需要修改三個XML,一個class:
1.第一個xml是布局XML文件(如:main.xml),是這個widget的。一般來說如果用這個部件顯示時間,那就只在這個布局XML中聲明一個textview就OK了。
2.第二個xml是widget_provider.xml,主要是用于聲明一個appwidget的。其中,Layout就是指定上面那個main.xml。
3.第三個xml是AndroidManifest.xml,注冊broadcastReceiver信息。
4.最后那個class用于做一些業(yè)務邏輯操作。讓其繼承類AppWidgetProvider。AppWidgetProvider中有許多方法,一般情況下我們只是覆寫onUpdate(Context,AppWidgetManager,int[])方法。
二、代碼案例
1.定義一個WidgetProvider, 用來處理Widget的一些CallBack
(1)OnEnable,創(chuàng)建第一個Widget時調(diào)用。
(2)OnDisable, 和OnEnable相反,創(chuàng)建最后一個Widget調(diào)用。
(3)OnDelete,Widget的一個實例被刪除時調(diào)用。
(4)OnUpdate,當Widget需要更新它的View時調(diào)用。
(5)onReceive():此方法默認情況下處理BroadcastReceiver行為,并調(diào)用上面的方法。
public class WidgetDemoAppWidgetProvider extends AppWidgetProvider{ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; // Perform this loop procedure for each App Widget that belongs to this provider for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_demo_layout); views.setOnClickPendingIntent(R.id.wap_app, pendingIntent); appWidgetManager.updateAppWidget(appWidgetId, views); } } }
2.在AndroidManifast.xml 注冊Provide
<receiver android:name="WidgetDemoAppWidgetProvider"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_demo_appwidget_info" /> </receiver>
3.創(chuàng)建Widget配置XML在xml文件夾下:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="60px" android:minHeight="60px" android:initialLayout="@layout/widget_demo_layout" > </appwidget-provider>
(4)創(chuàng)建Widget 的Layout
<?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" > <ImageView android:id="@+id/wap_app" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"/> </LinearLayout>
相關(guān)文章
Android View與Compose互相調(diào)用實例探究
這篇文章主要介紹了Android View與Compose互相調(diào)用,Compose 具有超強的兼容性,兼容現(xiàn)有的所有代碼,Compose 能夠與現(xiàn)有 View 體系并存,可實現(xiàn)漸進式替換2023-01-01Android基于wheelView實現(xiàn)自定義日期選擇器
這篇文章主要為大家詳細介紹了Android基于wheelView實現(xiàn)自定義日期選擇器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Android實現(xiàn)創(chuàng)意LoadingView動畫效果
這篇文章主要介紹了Android實現(xiàn)創(chuàng)意LoadingView動畫效果的相關(guān)資料,需要的朋友可以參考下2016-02-02Android11及以上文件讀寫權(quán)限申請詳細介紹
安卓11改變了此前安卓系統(tǒng)對于文件管理的規(guī)則,在安卓 11 上,文件讀寫變成了特殊權(quán)限,下面這篇文章主要給大家介紹了關(guān)于Android11及以上文件讀寫權(quán)限申請的相關(guān)資料,需要的朋友可以參考下2022-08-08Android 實現(xiàn)圖片模糊、高斯模糊、毛玻璃效果的三種方法
在前幾天寫過一個使用glide-transformations的方法實現(xiàn)高斯模糊的方法,今天偶然間有發(fā)現(xiàn)一個大神寫的另一個方法,感覺挺不錯的,分享一下2016-12-12item高度不同時Recyclerview獲取滑動距離的方法
這篇文章主要介紹了item高度不同時Recyclerview獲取滑動距離的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11