Android桌面组件App Widget完整案例

时间:2021-12-06 07:52:35

本文实例讲述了Android桌面组件App Widget用法。分享给大家供大家参考。具体如下:

这里模拟一个案例:把AppWidget添加到桌面后,点击AppWidget后AppWidget文本会轮回改变

main.xml布局文件:

?
1
2
3
4
5
6
7
8
9
10
<?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:id="@+id/tv"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="程序入口"
  android:textSize="50dip"/>
</LinearLayout>

res/xml/my_appwidget.xml布局文件:

?
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:minWidth="120dp"
 android:minHeight="60dp"
 android:updatePeriodMillis="1000"
 android:initialLayout="@layout/main">
</appwidget-provider>

清单文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ljq.activity" android:versionCode="1"
 android:versionName="1.0">
 <application android:icon="@drawable/icon"
  android:label="@string/app_name">
  <receiver android:name=".TestActivity">
   <meta-data android:name="android.appwidget.provider"
    android:resource="@xml/my_appwidget">
   </meta-data>
   <intent-filter>
    <action android:name="COM.LJQ.ACTION.WIDGET.CLICK"></action>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
   </intent-filter>
  </receiver>
 </application>
 <uses-sdk android:minSdkVersion="7" />
</manifest>

变量类UtilTool:用来控件文本改变:

?
1
2
3
4
package com.ljq.activity;
public class UtilTool {
 public static boolean isChange=true;
}

TestActivity类,继承自AppWidgetProvider:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.ljq.activity;
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 TestActivity extends AppWidgetProvider {
 // 自定义一个Action名
 private static final String ACTION_CLICK_NAME = "COM.LJQ.ACTION.WIDGET.CLICK";
 private RemoteViews rv;
 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  System.out.println("onUpdate");
  //获取R.layout.main布局,通过类RemoteViews对布局R.layout.main里的控件进行操作
  /*rv = new RemoteViews(context.getPackageName(), R.layout.main);
  Intent intentClick = new Intent(ACTION_CLICK_NAME);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);
  rv.setOnClickPendingIntent(R.id.tv, pendingIntent);
  ComponentName cmp = new ComponentName(context, TestActivity.class);
  AppWidgetManager myAppWidgetManager = AppWidgetManager.getInstance(context);
  myAppWidgetManager.updateAppWidget(cmp, rv);*/
  final int N = appWidgetIds.length;
  for (int i = 0; i < N; i++) {
   int appWidgetId = appWidgetIds[i];
   updateAppWidget(context, appWidgetManager, appWidgetId);
  }
 }
 //AppWidget生命周期: 每接收一次,广播执行一次为一个生命周期结束。
 //也就是说在重写AppWidgetProvider类里面声明全局变量做状态判断,
 //每次状态改变AppWidgetProvider再接收第二次广播时即为你重新初始化也就是说重新实例化了一次AppWidgetProvider。
 //今天我因为在里面放了一个boolean值初始化为true,观察调试看到每次进入都为TRUE故你在设置桌面组件时,
 //全局变量把它声明在另外一个实体类用来判断是没问题的,切忌放在本类。
 @Override
 public void onReceive(Context context, Intent intent) {
  System.out.println("onReceive");
  if (rv == null) {
   rv = new RemoteViews(context.getPackageName(), R.layout.main);
  }
  if (intent.getAction().equals(ACTION_CLICK_NAME)) {
   if (UtilTool.isChange) {
    rv.setTextViewText(R.id.tv, "abc");
   } else {
    rv.setTextViewText(R.id.tv, "123");
   }
   UtilTool.isChange = !UtilTool.isChange;
   AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(context);
   int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(context, TestActivity.class));
   appWidgetManger.updateAppWidget(appIds, rv);
  }else{
   super.onReceive(context, intent);
  }
 }
 private void updateAppWidget(Context context,
  AppWidgetManager appWidgeManger, int appWidgetId) {
  rv = new RemoteViews(context.getPackageName(), R.layout.main);
  Intent intentClick = new Intent();
  intentClick.setAction(ACTION_CLICK_NAME);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);
  rv.setOnClickPendingIntent(R.id.tv, pendingIntent);
  appWidgeManger.updateAppWidget(appWidgetId, rv);
 }
}

希望本文所述对大家的Android程序设计有所帮助。