读书人

Android Widget示范

发布时间: 2012-09-13 09:51:52 作者: rapoo

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

1)用于布局的组件类。

FrameLayout LinearLayout   RelativeLayout

2)可视组件类

AnalogClock ImageView ProgressBar TextView Button Chronometer ImageButton



view sourceprint?
1 创建Widget描述文件,该文件是Xml格式,必须放在res\xml目录中

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

3 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"

4 android:minWidth="309dip"

5 android:minHeight="192dip"

6 android:initialLayout="@layout/main" 初始化布局

7 android:updatePeriodMillis="1000" 更新的时间间隔(毫秒)

8 />
view sourceprint?
1 建立Widget类,该类必须从AppWidgetProvider类继承(AppWidgetProvider是BroadcastReceiver的子类,因此,Widget类可以接受广播消息)

2 在AndroidManifest.xml文件中定义widget

3 <receiver android:label="@string/app_name" android:name=".HelloWidget">

4 <intent-filter>

5 <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>

6 </intent-filter>

7 <meta-data android:name="android.appwidget.provider"

8 android:resource="@xml/hello_widget_provider"/>

9 </receiver>


view sourceprint?
01 显示时间,每秒更新

02 public class HelloWidget extends AppWidgetProvider {

03 private Timer timer;

04 private int[] appWidgetIds;

05 private AppWidgetManager appWidgetManager;

06 private Context context;

07

08 @Override

09 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

10 super.onUpdate(context, appWidgetManager, appWidgetIds);

11 Log.i("Widget", "onUpdate");

12 this.context = context;

13 this.appWidgetManager = appWidgetManager;

14 this.appWidgetIds = appWidgetIds;

15 timer = new Timer();

16 timer.schedule(task, 0,1000);

17 }

18 private Handler handler = new Handler(){

19 @Override

20 public void handleMessage(Message msg) {

21 super.handleMessage(msg);

22 switch (msg.what) {

23 case 1:

24 Time myTime = new Time();

25 myTime.setToNow();

26 RemoteViews updateViews =

27 new RemoteViews(context.getPackageName(),

28 R.layout.main);

29 updateViews.setTextViewText(R.id.widget_textview, "北京时间"+myTime.format("%H:%M:%S"));

30 Log.i("Widget", "北京时间"+myTime.format("%H:%M:%S"));

31 // ComponentName thisWidget = new ComponentName(context,HelloWidget.class);

32 // appWidgetManager.updateAppWidget(thisWidget, updateViews);

33 appWidgetManager.updateAppWidget(appWidgetIds, updateViews);

34 break;

35 default:

36 break;

37 }

38 }

39 };

40 private TimerTask task = new TimerTask() {

41 public void run() {

42 handler.sendEmptyMessage(1);

43 }

44 };

45 }
音乐播放widget

view sourceprint?01 package cn.stay.widget;

02

03 import android.app.PendingIntent;

04 import android.appwidget.AppWidgetManager;

05 import android.appwidget.AppWidgetProvider;

06 import android.content.Context;

07 import android.content.Intent;

08 import android.os.Handler;

09 import android.os.Message;

10 import android.widget.RemoteViews;

11 import cn.aoran.activity.R;

12

13 public class HelloWidget extends AppWidgetProvider {

14 private static final String WIDGET_BACK = "cn.stay.widget.back";

15 private static final String WIDGET_PLAY = "cn.stay.widget.play";

16 private static final String WIDGET_NEXT = "cn.stay.widget.next";

17 private static final String WIDGET_SONG = "cn.stay.widget.song";

18 private static final String WIDGET_CURRENT = "cn.stay.widget.current";

19 private String song = "";

20 private static Context context;

21 private static AppWidgetManager appWidgetManager;

22 private static int[] appWidgetIds;

23

24 @Override

25 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

26 super.onUpdate(context, appWidgetManager, appWidgetIds);

27 HelloWidget.context = context;

28 HelloWidget.appWidgetIds = appWidgetIds;

29 HelloWidget.appWidgetManager = appWidgetManager;

30 handler.sendEmptyMessage(0);

31 }

32

33 @Override

34 public void onReceive(Context context, Intent intent) {

35 super.onReceive(context, intent);

36 if (WIDGET_SONG.equals(intent.getAction())) {

37 song = intent.getStringExtra("song");

38 if (song == null || "".equals(song.trim())) {

39 song = "未知";

40 }

41 if (context != null) {

42 handler.sendEmptyMessage(1);

43 }

44 }

45 }

46

47 private Handler handler = new Handler() {

48 public void handleMessage(Message msg) {

49 RemoteViews widgetViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

50 super.handleMessage(msg);

51 switch (msg.what) {

52 case 0:

53 context.sendBroadcast(new Intent(WIDGET_CURRENT));

54 // Intent intent = new Intent(context,LocalActivity.class);

55 // PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);

56 // widgetViews.setOnClickPendingIntent(R.id.widget_sub, pIntent);

57 // 上一首

58 PendingIntent backIntent = PendingIntent.getBroadcast(context, 0, new Intent(WIDGET_BACK), 0);

59 widgetViews.setOnClickPendingIntent(R.id.widget_back_btn, backIntent);

60 // 下一首

61 PendingIntent nextIntent = PendingIntent.getBroadcast(context, 0, new Intent(WIDGET_NEXT), 0);

62 widgetViews.setOnClickPendingIntent(R.id.widget_next_btn, nextIntent);

63 // 播放

64 PendingIntent playIntent = PendingIntent.getBroadcast(context, 0, new Intent(WIDGET_PLAY), 0);

65 widgetViews.setOnClickPendingIntent(R.id.widget_play_btn, playIntent);

66

67 appWidgetManager.updateAppWidget(appWidgetIds, widgetViews);

68 break;

69 case 1:

70 widgetViews.setTextViewText(R.id.widget_song, song);

71 // ComponentName thisWidget = new ComponentName(context, HelloWidget.class);

72 // AppWidgetManager manager = AppWidgetManager.getInstance(context);

73 appWidgetManager.updateAppWidget(appWidgetIds, widgetViews);

74 break;

75 default:

76 break;

77 }

78 }

79

80 };

81 }

读书人网 >Android

热点推荐