android¿ª·¢ÖAPP Widget
android¿ª·¢ÖAPP Widget±¾²ÎÄÖ÷Òª½²ÊöµÄandroid¿ª·¢ÖеÄ×ÀÃæ³ÌÐòµÄ¿ª·¢--APP widget£¬Ö÷ÒªÓõÄÊÇPendingIntentºÍRemoteViews¡£PendingIntentÖ÷ÒªÓÃÀ´ÉèÖÃ×ÀÃæ³ÌÐòµÄÏàÓ¦·½Ê½¡£´Ë¶ÔÏó¿ÉÒÔÓÐÈýÖÖIntent·½Ê½£¬µÚÒ»¸öÊÇ¿ªÊ¼Ò»¸öеÄActivity£¬µÚ¶þ¸öÊÇ·¢ËÍÒ»¸ö¹ã²¥£¬µÚÈý¸öÊÇ¿ªÊ¼Ò»¸öservice¡£
RemoteViewsµÄ×÷ÓãºÒòΪ×ÀÃæ³ÌÐòºÍapp³ÌÐò²»ÊÇÊôÓÚÒ»¸ö½ø³Ì£¬²»ÄÜͨ¹ýÕý³£µÄ²Ù×÷¿Ø¼þµÄ·½Ê½À´²Ù×÷¡£ÐèҪʹÓÃϵͳÌá¹µÄRemoteViews¶ÔÏóÀ´²Ù×÷×ÀÃæ³ÌÐò¡£ÔÚÕâ¸öÀàÖÐÓÐÉèÖÃAPP WidgetÖпؼþÊôÐԵķ½·¨¡£
ÏÂÃæÎÒÃÇÀ´¿´¿´´úÂ룬ÔÚ´úÂëÖÐÓзֱð°üº¬ÁËÉÏÊöËù˵µÄPendingInetntµÄÁ½ÖÖ·½Ê½£¨part 1£º¿ªÊ¼Ò»¸öеÄActivity£»part 2£º·¢ËÍÒ»¸ö¹ã²¥£ºÍRemoteViewsµÄʹÓ÷½·¨£º
Ö÷³ÌÐòµÄActivity£¬MainActivity.java£º
package com.example.appwidget;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
appwidget×ÀÃæ³ÌÐòµÄÀàAppWidgetProviderTest.java£º
package com.example.appwidget;
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 AppWidgetProviderTest extends AppWidgetProvider {
//×Ô¶¨ÒåÒ»¸öACTION³£Á¿
private final String USER_ACTION = "com.example.appwiget.USER_ACTION";
//µ±APP widgetʵÀý±»É¾³ýʱ£¬»áµ÷ÓÃ
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
System.out.println("onDeleted()");
}
//µ±×îºóÒ»¸öapp widgetʵÀý±»É¾³ýʱ£¬»áµ÷ÓÃ
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
System.out.println("onDisabled()");
}
//µ±µÚÒ»¸öApp widgetʵÀý±»´´½¨Ê±£¬»áµ÷ÓÃ
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
System.out.println("onEnabled()");
}
//½ÓÊÕappÊÕµ½µÄËùÓй㲥ʼþ
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("onReceive()");
//±È½ÏÏ£¬Èç¹ûÊÕµ½µÄ¹ã²¥ÊÇUSER_ACTION£¬ÔòÊä³öÐÅÏ¢,ÇÒ¸üÐÂ×ÀÃæ¿Ø¼þ
if(USER_ACTION.equals(intent.getAction())){
System.out.println("onReceive --> " + USER_ACTION);
//»ñµÃRemoteViews¶ÔÏó
RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.appwidget_provider);
//¸üÐÂ×ÀÃæTextView¿Ø¼þµÄtext
remoteView.setTextViewText(R.id.myText, "onReceive");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, AppWidgetProviderTest.class);
appWidgetManager.updateAppWidget(componentName, remoteView);
}else{
//µ÷Óø¸ÀàµÄ·½·¨
super.onReceive(context, intent);
}
}
//ÔÚµ½´ïÖ¸¶¨µÄ¸üÐÂʱ¼äÖºó»òÕßÏò×ÀÃæÌí¼ÓApp widgetʵÀýʱ£¬»áµ÷Óô˷½·¨
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
System.out.println("onUpdate()");
//Ϊÿһ¸öapp widgetʵÀý°ó¶¨Ò»¸öremoteViews¼àÌýÆ÷£¬Ã¿´´½¨µÄÒ»¸öapp widgetʵÀý¶¼ÓÐÒ»¸ö¶ÔÓ¦µÄID
for(int i = 0; i < appWidgetIds.length; i ++){
System.out.println("appWidgetIds -->" + appWidgetIds[i]);
//´´½¨Ò»¸öIntent¶ÔÏó
Intent intent = new Intent();
//part 1£ºpendingIntentÓÃÀ´´ò¿ªÒ»¸öеÄActivity
/*intent.setClass(context, MainActivity.class);
//ʹÓÃgetActivity·½·¨´´½¨Ò»¸öpendingIntent¶ÔÏ󣬴˶ÔÏóÓÃÓÚÌø×ªActivity£¬½«intent´ò°ü¸øpendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
*/
//end part1
//part 2£ºpendingIntentÓÃÀ´·¢ËÍÒ»¸ö¹ã²¥
//ÉèÖÃaction
intent.setAction(USER_ACTION);
//ʹÓÃgetBroadcast()·½·¨´´½¨Ò»¸öpendingIntent¶ÔÏó
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
//end part2
//´´½¨Ò»¸öRemoteViews¶ÔÏó
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);
//RemoteViews¶ÔÏó½«ButtonºÍpendingIntent°ó¶¨¡£Í¨¹ýpendingIntent»ñµÃintent
remoteView.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteView);
}
}
}
Ö÷³ÌÐòµÄÖ÷²¼¾ÖÎļþmain.xml£º£¨³ÌÐòµÄÖ÷½çÃæ£ xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
app widgetµÄ½çÃæ²¼¾ÖÎļþappwidget_provider.xml£º£¨ÔÚ×ÀÃæ¿´µ½µÄ½çÃæ£
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="welcome appwidget"
android:background="#ff0000"
/>
android:id="@+id/WidgetButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Widget Button"
/>
appwidget_providerµÄÔªÊý¾ÝµÄÎļþappwidget_provider_info.xml£º
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="180dp"
android:minHeight="40dp"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/appwidget_provider"
>
AndroidManifest.xmlÎļþ£º
package="com.example.appwidget"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.appwidget.MainActivity"
android:label="@string/app_name" >
android:name="com.example.appwidget.AppWidgetProviderTest">
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_provider_info"/>
ʵÏÖµÄЧ¹ûÈçÏ£ºÖ÷³ÌÐò½çÃæ£º
<†·Ÿ"http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPgo8c3Ryb25nPmFwcCB3aWRnZXS958Pmo7o8L3N0cm9uZz4KPHN0cm9uZz48aW1nIHNyYz0="" alt="\">
app widgetÐèÒªÔÚ×ÀÃæÉÏÌí¼ÓÎÒÃÇ´´½¨µÄapp£¬²Å»áÔÚ×ÀÃæÉÏÏÔʾ³öÀ´¡£¿ÉÒÔ¿Õ°×´¦³¤°´£¬¾Í»á³öÀ´Ìí¼ÓwidgetÑ¡ÏѡÖÐÎÒÃǵÄapp¼È»áÔÚ×ÀÃæÏÔʾÉÏͼ¡£
ÈçÉϳÌÐò£¬µã»÷°´Å¥£¬¾Í»á·¢Ë͹㲥£¬²¢ÇÒÐÞ¸ÄwidgetÖеÄTextView¿Ø¼þµÄText£¬µã»÷ºóµÄЧ¹ûÈçÏ£º