国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之App widget用法實例分析

2019-12-12 06:09:39
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發之App widget用法。分享給大家供大家參考,具體如下:

放在桌面上的控件叫做――App widget,例如可以在桌面上添加按鈕、圖片等等控件,例如桌面播放器的控制面板

AppWidgetProviderInfo對象,它為App Widget提供元數據,包括布局、更新頻率等等數據,這個對象不是由我們自己生成的,而是由android自己定義配置完成,這個對象被定義在XML文件中

1、定義AppWidgetProviderInfo對象,在res/xml文件夾當中定義一個名為widget_config.xml文件

<?xml version="1.0" encoding="utf-8"?><appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"  android:minWidth="300dp"  android:minHeight="72dp"  android:updatePeriodMillis="0"  android:initialLayout="@layout/widget_ui"></appwidget-provider>

備注:建立的文件夾名一定是xml,因為只有這樣才能被R識別

2、AppWidgetProvider定義了App Widget的基本生命周期

public class MyWidgetProvider extends AppWidgetProvider {  public static int Tag;  public int max;  public int current;  @Override  public void onEnabled(Context context) {    super.onEnabled(context);    System.out.println("第一次被創建時調用這個方法");  }  @Override  public void onDisabled(Context context) {    System.out.println("當最后一個App Widget被刪除時調用該方法");  }  @Override  public void onReceive(Context context, Intent intent) {  //調用父類的onReceive方法不能少,否則就無法監聽到onUpdate事件了    super.onReceive(context, intent);    System.out.println("接收廣播事件");  }  @Override  public void onUpdate(Context context, AppWidgetManager appWidgetManager,      int[] appWidgetIds) {      System.out.println("在到達指定的更新時間之后或者當用戶向桌面添加App Widget時調用這個方法");      for(int i = 0; i < appWidgetIds.length; i++){        Intent intent = new Intent(context, HB.class);        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);        //R.layout.widget_ui指的是顯示在桌面上的控件布局        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_ui);        //R.id.widgetButton指的是為桌面控件按鈕綁定事件        remoteViews.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);        //updateAppWidget方法更新remoteViews        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);      }    }  }  @Override  public void onDeleted(Context context, int[] appWidgetIds){    System.out.println("App Widget被刪除時調用這個方法");  }}

3、添加一個布局文件res/layout/widget_ui.xml(在桌面上顯示的內容)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">  <Button  android:id="@+id/widget_BT_Up"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="Value++"/>  <Button android:id="@+id/widget_BT_Down"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="Value--"  android:layout_weight="1"/></LinearLayout>

4、在AndroidManifest.xml文件中添加reseiver標簽

android:resource="@xml/widget_config" 指明顯示widget_config.xml是appwidget的屬性初始化設置
android:name="android.appwidget.action.APPWIDGET_UPDATE" 是android系統提供判定是appwidget的處理方式
android:name=".MyWidgetProvider" 表示處理的類,即繼承了AppWidgetProvider類的類

<receiver android:name=".MyWidgetProvider" android:label="myWIdget" android:icon="@drawable/icon">  <intent-filter>    <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>  </intent-filter>  <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config"/></receiver>

備注:App Widget和我們應用程序運行在不同的進程中(App Widget當中的View運行在Home Screen進程中),因此要用到RemoteViews和PendingIntent這兩個類來操控桌面的控件

如果你的onDelete、onUpdate等事件沒有觸發,那么一個重要的原因是,你override了onReceive事件,但是又沒有調用super.onReceive(),所以導致這之后的事件都不會觸發,AppWidgetProvider的事件處理機制是,onRecieve首先觸發,然后由onReceive去觸發后續事件。

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴林左旗| 龙岩市| 马山县| 巍山| 泗阳县| 盘锦市| 白城市| 湟源县| 巧家县| 遂平县| 五家渠市| 九寨沟县| 瑞金市| 海林市| 沿河| 广饶县| 临夏县| 芜湖市| 云龙县| 灵寿县| 梧州市| 青岛市| 治县。| 房产| 雷州市| 三河市| 井陉县| 青龙| 长泰县| 汉源县| 遂溪县| 孟津县| 嵊州市| 安多县| 吉木萨尔县| 望奎县| 佛冈县| 达尔| 左云县| 连平县| 镇赉县|