Android--案頭組件AppWidget

來源:互聯網
上載者:User

AppWidget通常被稱為案頭外掛程式,是Android系統應用開發層上的重要部分,是把一個進程的控制項嵌入到別外一個進程的視窗裡的一種方法,是與用戶端程式互動的組件程式。

先看看:

下面進行執行個體記錄如何建立AppWidget,它是如何與用戶端程式進行互動:

第一步:在res目錄下建立xml檔案夾並建立<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+eXd4X2FwcHdpZGdldF9pbmZvLnhtbDwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">
第二步:建立布局檔案ywx_appwidget.xml

                              

第三步:建立一個ExampleAppWidgetProvider.java,實現AppWidgetProvider中的方法

package com.ywx.appwidget_04;import android.app.PendingIntent;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.widget.RemoteViews;public class ExampleAppWidgetProvider extends AppWidgetProvider{private static final String UPDATE_ACTION = "com.ywx.appwidget_04.UPDATE_APP_WIDGET";//接收廣播事件@Overridepublic void onReceive(Context context, Intent intent) {super.onReceive(context, intent);}//在到達指定的更新時間之後或者當使用者向案頭添加App Widget時會調用該方法@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {super.onUpdate(context, appWidgetManager, appWidgetIds);}   //當AppWidget被刪除時,會調用該方法@Overridepublic void onDeleted(Context context, int[] appWidgetIds) {super.onDeleted(context, appWidgetIds);}//當一個AppWidget的執行個體第一次被建立時會調用該方法@Overridepublic void onEnabled(Context context) {super.onEnabled(context);}//當最後一個AppWidget執行個體被刪除後調用該方法@Overridepublic void onDisabled(Context context) {super.onDisabled(context);}}


第四步:在AndroidManifest.xml中聲明:這裡有兩個actiong,一個是系統的,一個是自訂的,只要滿足任何一個action就會觸發事件

                                                                                                    

完成以上四步就能夠完成簡單的AppWidget樣本。
在AppWidget開發過程中主要運用到了PendingIntent和RemoteViews,下面將用法記錄如下:
PendingIntent主要的三個方法:
1.getActivity(Context context,int requestCode,Intent intent,int flags)
2.getBroadcast(Context context,int requestCode,Intent intent,int flags)
3.getService(Context context,int requestCode,Intent intent,int flags)
RemoteViews的作用:表示一系列的view對象及所表示的對象運行在另外的進程當中。
appwidget組件程式並不是處在當前的進程當中,所以對於監聽器的綁定事件與原來的有所區別:
在onUpdate()方法中添加

Intent intent = new Intent();intent.setAction(UPDATE_ACTION);//使用getBroadcast產生PendingIntent,當對象執行時,發送廣播PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.ywx_appwidget);remoteViews.setOnClickPendingIntent(R.id.widgetBut, pendingIntent);appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

在onRecive()方法中添加
String action = intent.getAction();if(UPDATE_ACTION.equals(action)){RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.ywx_appwidget);remoteViews.setImageViewResource(R.id.imageId, R.drawable.upload);remoteViews.setTextViewText(R.id.widgetText, "娃哈哈");AppWidgetManager appWidgeManager = AppWidgetManager.getInstance(context);ComponentName componentName = new ComponentName(context,ExampleAppWidgetProvider.class);appWidgeManager.updateAppWidget(componentName, remoteViews);}else{super.onReceive(context, intent);}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.