Android學習筆記之AppWidget

來源:互聯網
上載者:User

android案頭控制項:

appwidget當中的View運行在Home Screen進程中,和我們的應用不在同一進程中。
AppWidgetProviderInfo對象:
為App Widget提供中繼資料,包括布局,更新頻率等,這個對象被定義在xml檔案中。

AppWidgetProvider:定義了AppWidget的基本生命週期函數

方法:onUpdate:在到達指定的更新時間之後後者當使用者向案頭添加App Widget時會調用
該方法。
onDeleted:當App Widget被刪除時,會調用該方法
onEnabled:當一個App Widget的執行個體第一次被建立時調用
onDisabled:當最後一個App Widget執行個體被刪除後,會調用該方法。
onReveice:接收廣播事件

步驟:
1,在res目錄下建立目錄xml,在裡面建立檔案widget_info.xml檔案。
<?xml version="1.0" encoding="utf-8" ?>

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

android:initialLayout="@layout/appwidget">
    
</appwidget-provider>

2,在layout下建立布局檔案appwidget.xml檔案:

<?xml version="1.0" encoding="utf-8" ?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<TextView 
    android:id="@+id/widgetTextld"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="firstWidgetText"
    android:background="#000000"
   ></TextView>
    
</LinearLayout>
3,AndroidManifest.xml檔案中加receiver標籤:
<receiver android:name="AppWidgetProviderTest">
            <intent-filter >
                <action 

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

></meta-data>
     </receiver>
4,建立類:AppWidgetProviderTest.java,繼承自AppWidgetProvider實現方法。
package com.gufengxiachen.android;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;

public class AppWidgetProviderTest extends AppWidgetProvider{
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("Deleted");
super.onDeleted(context, appWidgetIds);
}

@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
System.out.println("Enabled");
super.onEnabled(context);
}

@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
System.out.println("Disabled");
super.onDisabled(context);
}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("Receive");
super.onReceive(context, intent);
}

@Override
public void onUpdate(Context context, AppWidgetManager 

appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("Update");
super.onUpdate(context, appWidgetManager, appWidgetIds);
}

}

PendingIntent:封裝暫時不用的Intent,順延強制的Intent。主要和Android特定的的遠

程服務打交道(簡訊、通知、鬧鈴等),通常的應用無需使用。
構造PendingIntent對象:
靜態方法:
getActivity(Content content,int requestCode,Intent intent,int flags);
getBroadcast(Content content,int requestCode,Intent intent,int flags);
getService(Content content,int requestCode,Intent intent,int flags);

RemoteViews:表示一系列的View對象。RemoteViews啟動並執行對象在另外一個進程中。   
    
    
綁定appwidget的控制項監聽器:
remoteViews.setOnClickPendingIntent(R.id.widgetButtonld,pendingIntent);

主要代碼:
for(int i=0;i<appWidgetIds.length;i++){
System.out.println(appWidgetIds[i]);
//定義intent對象
Intent intent = new Intent

(context,TargetActivity.class);
//定義PendingIntent對象,封裝Intent對象
PendingIntent pendingIntent = 

PendingIntent.getActivity(context, 0, intent, 0);
//擷取RemoteViews對象
RemoteViews remoteViews = new RemoteViews

(context.getPackageName(),R.layout.appwidget);
//控制項添加監聽器
remoteViews.setOnClickPendingIntent

(R.id.widgetbuttonld, pendingIntent);
//更新App Widget
appWidgetManager.updateAppWidget(appWidgetIds[i], 

remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);

}

接收來自AppWidget的廣播:
1,在AndroidManifest.xml中為AppWidgetProvider註冊新的intent-filter;
2,使用getBroadcast()方法建立一個PendingIntent;
3,為AppWidget中的控制項註冊處理器
4,在onReceive()方法中接收廣播資訊。    

聯繫我們

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