posts - 71,  comments - 203,  trackbacks - 0

Widget并不支持所有的Android组件,只能在Widget中使用如下组件类:

1)用于布局的组件类。

FrameLayout   LinearLayout   RelativeLayout

2)可视组件类

AnalogClock  ImageView   ProgressBar   TextView   Button  Chronometer  ImageButton

 

创建Widget描述文件,该文件是Xml格式,必须放在res\xml目录中
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="309dip"
    android:minHeight="192dip"
    android:initialLayout="@layout/main"	初始化布局
    android:updatePeriodMillis="1000"		更新的时间间隔(毫秒)
/>
建立Widget类,该类必须从AppWidgetProvider类继承(AppWidgetProvider是BroadcastReceiver的子类,因此,Widget类可以接受广播消息)
在AndroidManifest.xml文件中定义widget
<receiver android:label="@string/app_name" android:name=".HelloWidget">
			<intent-filter>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
			</intent-filter>
			<meta-data android:name="android.appwidget.provider"
				android:resource="@xml/hello_widget_provider"/>
</receiver>
显示时间,每秒更新
public class HelloWidget extends AppWidgetProvider {
	private Timer timer;
	private int[] appWidgetIds;
	private AppWidgetManager appWidgetManager;
	private Context context;
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		Log.i("Widget", "onUpdate");
		this.context = context;
		this.appWidgetManager = appWidgetManager;
		this.appWidgetIds = appWidgetIds;
		timer = new Timer();
		timer.schedule(task, 0,1000);
}
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				Time myTime = new Time();
		        myTime.setToNow();
		        RemoteViews updateViews = 
		            new RemoteViews(context.getPackageName(),
		              R.layout.main);
		        updateViews.setTextViewText(R.id.widget_textview, "北京时间"+myTime.format("%H:%M:%S"));
		        Log.i("Widget", "北京时间"+myTime.format("%H:%M:%S"));
//		        ComponentName thisWidget = new ComponentName(context,HelloWidget.class);
//		        appWidgetManager.updateAppWidget(thisWidget, updateViews);
		        appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
				break;
			default:
				break;
			}
		}
	};
	private TimerTask task = new TimerTask() {
		public void run() {
			handler.sendEmptyMessage(1);
		}
	};
}

音乐播放widget

package cn.stay.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.RemoteViews;
import cn.aoran.activity.R;

public class HelloWidget extends AppWidgetProvider {
	private static final String WIDGET_BACK = "cn.stay.widget.back";
	private static final String WIDGET_PLAY = "cn.stay.widget.play";
	private static final String WIDGET_NEXT = "cn.stay.widget.next";
	private static final String WIDGET_SONG = "cn.stay.widget.song";
	private static final String WIDGET_CURRENT = "cn.stay.widget.current";
	private String song = "";
	private static Context context;
	private static AppWidgetManager appWidgetManager;
	private static int[] appWidgetIds;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		HelloWidget.context = context;
		HelloWidget.appWidgetIds = appWidgetIds;
		HelloWidget.appWidgetManager = appWidgetManager;
		handler.sendEmptyMessage(0);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		if (WIDGET_SONG.equals(intent.getAction())) {
			song = intent.getStringExtra("song");
			if (song == null || "".equals(song.trim())) {
				song = "未知";
			}
			if (context != null) {
				handler.sendEmptyMessage(1);
			}
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			RemoteViews widgetViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
			super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				context.sendBroadcast(new Intent(WIDGET_CURRENT));
				// Intent intent = new Intent(context,LocalActivity.class);
				// PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
				// widgetViews.setOnClickPendingIntent(R.id.widget_sub, pIntent);
				// 上一首
				PendingIntent backIntent = PendingIntent.getBroadcast(context, 0, new Intent(WIDGET_BACK), 0);
				widgetViews.setOnClickPendingIntent(R.id.widget_back_btn, backIntent);
				// 下一首
				PendingIntent nextIntent = PendingIntent.getBroadcast(context, 0, new Intent(WIDGET_NEXT), 0);
				widgetViews.setOnClickPendingIntent(R.id.widget_next_btn, nextIntent);
				// 播放
				PendingIntent playIntent = PendingIntent.getBroadcast(context, 0, new Intent(WIDGET_PLAY), 0);
				widgetViews.setOnClickPendingIntent(R.id.widget_play_btn, playIntent);

				appWidgetManager.updateAppWidget(appWidgetIds, widgetViews);
				break;
			case 1:
				widgetViews.setTextViewText(R.id.widget_song, song);
//				ComponentName thisWidget = new ComponentName(context, HelloWidget.class);
//				AppWidgetManager manager = AppWidgetManager.getInstance(context);
				appWidgetManager.updateAppWidget(appWidgetIds, widgetViews);
				break;
			default:
				break;
			}
		}

	};
}

 

 

 

posted on 2010-12-08 09:58  stay  阅读(1155)  评论(0编辑  收藏