Android App Widget的簡單使用

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   使用   

  App Widget是一些案頭的小外掛程式,比如說天氣和某些音樂播放應用,放到案頭去的那部分;

例如:

 

實現步驟及代碼如下:

(1)首先,在AndroidManifest.xml中聲明一個App Widget;

(1)定義AppWidgetProviderInfo對象:為App Widget提供中繼資料,包括布局、更新頻率等,這個對象定義在XML檔案當中;

  在res/xml檔案夾中定義一個名為example_appwidget_info.xml的檔案;

(2)為App Widget指定樣式和布局:

  定義一個新的布局檔案example_appwidget.xml;

(3)實現AppWidgetProvider:定義了App Widget的基本生命週期函數;

  onUpdate:在到達指定的更新時間之後或目前使用者向案頭添加App Widget時調用該方法;

  onDeleted:當App Widget被刪除時,會調用該方法;

  onEnabled:當一個App Widget的執行個體第一次被建立時,會調用該方法;

  onDisabled:當最後一個App Widget執行個體被刪除後,會調用該方法;

  onReveice:接收廣播事件;

 

詳細實現步驟可參考官方文檔,講解的十分詳細:

http://android.toolib.net/guide/topics/appwidgets/index.html

http://developers.androidcn.com/guide/topics/appwidgets/index.html#Providers

 

代碼:

 AndroidManifest.xml中聲明 App Widget:

<receiver android:name="ExampleAppWidgetProvider" >            <intent-filter>                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />            </intent-filter>            <meta-data                android:name="android.appwidget.provider"                android:resource="@xml/example_appwidget_info" />        </receiver>

 

res/xml目錄下的example_appwidget_info.xml:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:initialLayout="@layout/example_appwidget"    android:minHeight="294dp"    android:minWidth="72dp"    android:updatePeriodMillis="86400000" ></appwidget-provider>

 

新的布局檔案 example_appwidget.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/widgetTextId"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#00FFCC"        android:text="widget是一些案頭的小外掛程式" /></LinearLayout>

 

定義類ExampleAppWidgetProvider繼承AppWidgetProvider:

package com.example.appwidgettest2;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;public class ExampleAppWidgetProvider extends AppWidgetProvider {    @Override    public void onUpdate(Context context, AppWidgetManager appWidgetManager,            int[] appWidgetIds) {        System.out.println("onUpdate");        super.onUpdate(context, appWidgetManager, appWidgetIds);    }    @Override    public void onDeleted(Context context, int[] appWidgetIds) {        System.out.println("onDeleted");        super.onDeleted(context, appWidgetIds);    }    @Override    public void onDisabled(Context context) {        System.out.println("onDisabled");        super.onDisabled(context);    }    @Override    public void onEnabled(Context context) {        System.out.println("onEnabled");        super.onEnabled(context);    }}

 

 

刪除一個App Widget:

 

全部刪除:

 

在此基礎上,來看另一個例子:

給原先的App Widget添加一個按鈕,點擊後能跳轉到另一個Activity;

 

 

 

實現方式為:

  使用PendingIntent和RemoteViews來實現;

(1)建立PendingIntent的方法有3種;

 

 getActivity(Context context, int requestCode, Intent intent, int flags) getBroadcast(Context context, int requestCode, Intent intent, int flags) getService(Context context, int requestCode, Intent intent, int flags)

(2)RemoteViews的作用:

  RemoteViews對象表示了一系列的View對象;

  RemoteViews所代表的對象運行在另外的線程當中;

(3)因為App Widget和我們的應用程式運行在不同的進程當中(App Widget當中的View運行在Home Screen進程當中),所以無法按照之前那種方式綁定監聽器;

  而是應該使用remoteViews.setOnClickPendingIntent(R.id.widgetButtonId,pendingIntent)來實現;

 

TargetActivity.java

package com.example.appwidgettest2;import android.app.Activity;import android.os.Bundle;public class TargetActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.target_activity);    }}

 

target_activity.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="20sp"        android:gravity="center"        android:text="跳轉到這個Activity" /></LinearLayout>

 

而ExampleAppWidgetProvider修改為:

package com.example.appwidgettest2;import android.app.PendingIntent;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;import android.content.Intent;import android.widget.RemoteViews;public class ExampleAppWidgetProvider extends AppWidgetProvider {    @Override    public void onUpdate(Context context, AppWidgetManager appWidgetManager,            int[] appWidgetIds) {        System.out.println("onUpdate");        for (int i = 0; i < appWidgetIds.length; i++) {            System.out.println(appWidgetIds[i]);            // 建立一個Intent對象            Intent intent = new Intent(context, TargetActivity.class);            // 建立一個PendingIntent            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,                    intent, 0);            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),                    R.layout.example_appwidget);            // 為按鈕綁定事件處理器,            // 第一個參數用來指定被綁定處理器控制項的ID;            // 第二個參數用來指定當事件發生時,哪個PendingIntent將會被指定;            remoteViews.setOnClickPendingIntent(R.id.widgetButtonId,                    pendingIntent);            // 更新AppWidget            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);        }        super.onUpdate(context, appWidgetManager, appWidgetIds);    }    @Override    public void onDeleted(Context context, int[] appWidgetIds) {        super.onDeleted(context, appWidgetIds);    }    @Override    public void onDisabled(Context context) {        super.onDisabled(context);    }    @Override    public void onEnabled(Context context) {        super.onEnabled(context);    }}

 

Android App Widget的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.