android入門之App Widget(三)

來源:互聯網
上載者:User

 1、接收來自AppWidget的廣播
 1)、在AndroidManifest.xml當中為AppWidgetProvider註冊新的intent-filter
 2)、使用getBroadcast()方法建立一個PendingIntent
 3)、為AppWidget當中的控制項註冊處理器
 4)、在onReceive()方法當中接收廣播訊息
2、更新AppWidget當中控制項的狀態
注意:由於AppWidget和主程式不在同一個進程當中,不能用普通的方法進行操作!
 1)、在RemoteViews類當中有一系列的方法可以更新控制項
 2)、在使用RemoteViews更新控制項狀態之後,需要使用AppWidgetManager通知AppWidget進行更新。
下面請看例子:AppWidget中有TextView和ImageView和Button 3個控制項,我們的目的是通過Button來改變TextView和ImageView的內容。
先看布局檔案:res/layout檔案夾下:
main.xml內容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>
test.xml內容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按鈕"
    android:id="@+id/button"
    />   
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="你好"
    android:id="@+id/textView"
    />
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/hong"
    android:id="@+id/imageView"
    />
</LinearLayout>
res/xml檔案夾下info.xml內容如下:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
 android:minWidth="294dp"
 android:minHeight="72dp"
 android:updatePeriodMillis="30000"
 android:initialLayout="@layout/test"
 >
</appwidget-provider>
在AndroidManifest.xml檔案的application節點下添加以下內容:
<receiver android:name=".AppWidgetProviderTest">
   <intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
   </intent-filter>
   <intent-filter>
    <action android:name="weiyong.huang.APPWIDGET_UPDATE"/>
   </intent-filter>
   <meta-data android:name="android.appwidget.provider"
     android:resource="@xml/info"></meta-data>
</receiver>
還要在res/drawable檔案夾下添加兩張圖片,我命名兩張圖片為:bai.png和hong.png。
設定檔到此結束了,我我們下面來看java代碼:
建立一個AppWidgetProviderTest類,它繼承自AppWidgetProvider類。
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class AppWidgetProviderTest extends android.appwidget.AppWidgetProvider {
       //自訂的Intent的Action
 private static final String APP_ACTION = "weiyong.huang.APPWIDGET_UPDATE";
 @Override
 public void onReceive(Context context, Intent intent) {
  if(APP_ACTION.equals(intent.getAction())){
   System.out.println("weiyong.huang.APPWIDGET_UPDATE");
   RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.test);
   remoteViews.setTextViewText(R.id.textView,"哈哈");
   remoteViews.setImageViewResource(R.id.imageView, R.drawable.bai);
   AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
   ComponentName cn = new ComponentName(context,AppWidgetProviderTest.class);
   appWidgetManager.updateAppWidget(cn, remoteViews);
  }else{
   super.onReceive(context, intent);
  }
 }

 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager,
   int[] appWidgetIds) {
  Intent intent = new Intent();
  intent.setAction(APP_ACTION);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.test);
  remoteViews.setOnClickPendingIntent(R.id.button, pendingIntent);
  appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
  
  super.onUpdate(context, appWidgetManager, appWidgetIds);
 }

 @Override
 public void onDeleted(Context context, int[] appWidgetIds) {
  
  super.onDeleted(context, appWidgetIds);
 }

 @Override
 public void onEnabled(Context context) {
  
  super.onEnabled(context);
 }

 @Override
 public void onDisabled(Context context) {
  
  super.onDisabled(context);
 }
}

到此這個例子就算完成了,通過上面3篇的學習,基本學習了製作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.