国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android開發之AppWidget詳解

瀏覽:4日期:2022-09-19 16:39:29

Android通知系統是它的一大特色,而其中,AppWidget是其中一個亮點。在開發應用的中,很多時候可以為其添加一個AppWidget顯示在桌面中,及時方便的與用戶進行

交互。這里就簡單的熟悉一下開發一個AppWidget的流程吧。

想要在應用中創建一個AppWidget,至少需要以下幾樣東西:

需要創建一個AppWidgetProviderInfo,來描述AppWidget的元數據。 需要實現一個自己的AppWidgetProvider對AppWidget進行更新等操作。 需要布局文件來描述AppWidget的布局。

那么,下面就開始創建一個AppWidget吧。

一、在AndroidManifest.xml中聲明一個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實際上就是一個BroadcastReceiver,它接收特定的Broadcast。<meta-data>標簽描述了AppWidget所使用的元數據,android:resource則聲明了定義元數據的xml文件的位置。

二、添加AppWidgetProviderInfo元數據

AppWidgetProviderInfo描述了AppWidget的本質特性,例如,AppWidget更新的周期,最小的寬度、長度,所使用的布局文件是什么,以及添加AppWidget需要啟動的

configuration Activity等。我們需要在XML中來定義AppWidgetProviderInfo對象,這個XML文件應該保存在res/xml文件夾下。下面是一個范例:

<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>需要使用這個標簽來定義AppWidgetProviderInfo。下面對范例中使用到的屬性做下說明。

minWidthminHeight定義了AppWidget需要占據的最小的空間。

updatePeriodMillis定義了大概多久AppWidget需要更新一次,這里定義的只是一個大概的時間,系統不能做出精確的保證。

previewImage定義了在用戶選擇AppWidget時做現實的圖標。

initialLayout定義了AppWidget所使用的布局文件。

configure定義了AppWidget在添加的時候需要啟動的configuration Activity 用于執行配置的工作。

resizeMode定義了縮放模式。

三、創建AppWidget所使用的布局文件

在創建AppWidget時必須創建一個布局文件,為其提供布局描述。AppWidget創建視圖時,需要根據RemoteViews來創建。而出于效率等因素的考慮,很多控件在

RemoteViews中是被支持的。以下列出能在RemoteViews中使用的UI控件:

layout : FrameLayout , LinearLayout , RelativeLayout

widget : AnalogClock , Button , Chronometer , ImageButton , ImageView , ProgressBar , TextView , ViewFlipper , ListView , GridView , StackView , AdapterViewFlipper

四、創建一個AppWidgetProvider的子類

前面提到過AppWidgetProvider就是一個BroadcastReceiver。對,它其實確實是繼承自BroadcastReceiver,只是它為了更加方便的處理AppWidget的廣播進行了封裝。

AppWidgetProvider在接收到AppWidget的廣播的時候,會根據類型分別觸發以下幾個方法:

onUpdate() : 當AppWidget需要更新時,會觸發這個方法,我們需要重寫這個方法,在里面實現更新的操作。如果沒有定義configuration Activity,那么在添加一個AppWidget

時,也會觸發此方法。

onDelete(Context , int[] ):當AppWidget從AppWidgetHost中刪除時,會觸發此方法。

onEnabled(Context ):如果為一個應用添加了多個AppWidget,只有在第一個AppWidget被添加時,此方法才會被調用。

onDisabled(Context ):當一個應用的最后一個AppWidget從AppWidgetHost中刪除時,會觸發此方法。

onReceive(Context , Intent ):這實際上就是BroadcastReceiver中的方法,當任何一個Broadcast被接收到時,會調用此方法,并且會在以上回調方法之前被調用。

五、創建一個ConfigurationActivity(可選)

如果需要AppWidget添加的時候做一些配置工作,就可以使用Configuration Activity。要使用ConfigurationActivity首先需要像普通的Activity一樣在AndroidManifest.xml中

進行聲明:

<activity android:name='.ExampleAppWidgetConfigure'> <intent-filter> <action android:name='android.appwidget.action.APPWIDGET_CONFIGURE'/> </intent-filter> </activity>

只是這里需要添加action類型為android.appwidget.action.APPWIDGET_CONFIGURE的intent-filter。然后,需要在AppWidgetProviderInfo中進行聲明:

<appwidget-provider xmlns:android='http://schemas.android.com/apk/res/android' ... android:configure='com.example.android.ExampleAppWidgetConfigure' ... > </appwidget-provider>

最后,當然是需要創建Activity了,在Configuration Activity中,需要執行一些必要的操作:

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、進行必要的配置操作,獲取AppWidgetManager實例、更新RemoteViews

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget); appWidgetManager.updateAppWidget(mAppWidgetId, views);

3、設置Activity result,并且返回一個Intent。

Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); setResult(RESULT_OK, resultValue); finish();

這樣一個就創建好了一個Configuration Activity了。

注意android8.0以后無法收到發給自己的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);

執行完上面的步驟,就已經創建了一個可以在桌面進行顯示的AppWidget了。

以上就是Android開發之AppWidget詳解的詳細內容,更多關于Android AppWidget詳解的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
主站蜘蛛池模板: 三级视频中文字幕 | 毛片在线播放网站 | 成年人三级网站 | 久久99久久精品国产只有 | 久久久一区二区三区不卡 | 免费一级a毛片免费观看欧美大片 | 国产精品视频久久久久久 | 在线免费一区二区 | 国产特黄一级毛片特黄 | 精品日韩一区二区三区 | 国内精品一区二区三区最新 | 97在线播放视频 | 日本毛片在线 | 草久网 | 亚洲国产一区在线精选 | 成人免费在线 | 久久免费视频在线观看30 | 国产在线精品一区免费香蕉 | 看成年女人免费午夜视频 | 亚洲性视频网站 | 亚洲一级在线观看 | 久草视| 国产精品亚洲高清一区二区 | 久久一本一区二区三区 | 久久久国产成人精品 | 欧美视频精品一区二区三区 | 欧美日韩亚洲高清不卡一区二区三区 | 亚洲bbbbbxxxxx精品三十七 | 亚洲男女网站 | 秘书高跟黑色丝袜国产91在线 | 久久精品视频99精品视频150 | 欧美在线日韩在线 | 精品视频99 | 一区二区三区视频免费观看 | 久久精品免视看国产明星 | 手机免费毛片 | 91精品国产免费 | 丁香婷婷影音先锋5566 | 91久久香蕉国产线看观看软件 | 亚洲精品精品一区 | 亚欧成人毛片一区二区三区四区 |