簡單掌握Android Widget桌面小部件的創(chuàng)建步驟
一、Widget設(shè)計(jì)步驟
需要修改三個(gè)XML,一個(gè)class:
1.第一個(gè)xml是布局XML文件(如:main.xml),是這個(gè)widget的。一般來說如果用這個(gè)部件顯示時(shí)間,那就只在這個(gè)布局XML中聲明一個(gè)textview就OK了。
2.第二個(gè)xml是widget_provider.xml,主要是用于聲明一個(gè)appwidget的。其中,Layout就是指定上面那個(gè)main.xml。
3.第三個(gè)xml是AndroidManifest.xml,注冊broadcastReceiver信息。
4.最后那個(gè)class用于做一些業(yè)務(wù)邏輯操作。讓其繼承類AppWidgetProvider。AppWidgetProvider中有許多方法,一般情況下我們只是覆寫onUpdate(Context,AppWidgetManager,int[])方法。
二、代碼案例
1.定義一個(gè)WidgetProvider, 用來處理Widget的一些CallBack
(1)OnEnable,創(chuàng)建第一個(gè)Widget時(shí)調(diào)用。
(2)OnDisable, 和OnEnable相反,創(chuàng)建最后一個(gè)Widget調(diào)用。
(3)OnDelete,Widget的一個(gè)實(shí)例被刪除時(shí)調(diào)用。
(4)OnUpdate,當(dāng)Widget需要更新它的View時(shí)調(diào)用。
(5)onReceive():此方法默認(rèn)情況下處理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)文章
浮動(dòng)AppBar中的textField焦點(diǎn)回滾問題解決
這篇文章主要為大家介紹了浮動(dòng)AppBar中的textField焦點(diǎn)回滾問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android 使用 Path 實(shí)現(xiàn)搜索動(dòng)態(tài)加載動(dòng)畫效果
這篇文章主要介紹了Android 使用 Path 實(shí)現(xiàn)搜索動(dòng)態(tài)加載動(dòng)畫效果,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-08-08
Android源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法
這篇文章主要介紹了Android源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法,用idegen來生成針對AndroidStudio或IntelliJ?IDEA的Android系統(tǒng)源代碼工程配置文件,需要的朋友可以參考下2022-08-08
Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法
這篇文章主要給大家介紹了關(guān)于Android如何將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法,文中通過示例代碼介紹的非常吸納關(guān)系,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Android設(shè)置鈴聲實(shí)現(xiàn)代碼
這篇文章主要介紹了Android設(shè)置鈴聲實(shí)現(xiàn)代碼,以實(shí)例形式分析了Android中鈴聲設(shè)置的相關(guān)技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-10-10

