Android 开发之实时更新 App Widget

(转载)http://glgjing.github.io/blog/2015/11/12/android-kai-fa-zhi-shi-shi-geng-xin-app-widget/

Android 开发之 App Widget 详解中说到 updatePeriodMills 定义了 Widget 的刷新频率,但是出于节约用户电量的考虑,Android 系统默认最小更新周期是 30 分钟,也就是说:如果您的程序需要实时更新数据,设置这个更新周期是 2 秒,那么您的程序是不会每隔 2 秒就收到更新通知的,而是要等到 30 分钟以上才可以,要想实时的更新 Widget,一般可以采用 Service 和 AlarmManager 对 Widget 进行更新。

利用 Service 更新 Widget

在 onUpdate() 方法中启动 Service:

1
2
3
4
5
6
7
public class MyWidgetProvider extends AppWidgetProvider {

  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    context.startService(new Intent(context, WidgetService.class));
  }
}

在 Service 中对 Widget 进行更新,这里 Service 利用 AlarmManager 每隔一段时间进行自启,防止 Service 被系统 Kill 掉后无法对 Widget 进行更新,转载请注明出处:http://glgjing.github.io/

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
public class WidgetService extends Service {
  private static final int ALARM_DURATION  = 5 * 60 * 1000; // service 自启间隔
  private static final int UPDATE_DURATION = 10 * 1000;     // Widget 更新间隔
  private static final int UPDATE_MESSAGE  = 1000;

  private UpdateHandler updateHandler; // 更新 Widget 的 Handler

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    // 每个 ALARM_DURATION 自启一次
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntent = new Intent(getBaseContext(), WidgetService.class);
    PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), 0,
      alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
      SystemClock.elapsedRealtime() + ALARM_DURATION, pendingIntent);

    return START_STICKY;
  }

  @Override
  public void onCreate() {
    super.onCreate();

    Message message = updateHandler.obtainMessage();
    message.what = UPDATE_MESSAGE;
    updateHandler = new UpdateHandler();
    updateHandler.sendMessageDelayed(message, UPDATE_DURATION);
  }

  private void updateWidget() {
    // 更新 Widget
    RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetManager.updateAppWidget(new ComponentName(context, MyWidgetProvider.class), remoteViews);

    // 发送下次更新的消息
    Message message = updateHandler.obtainMessage();
    message.what = UPDATE_MESSAGE;
    updateHandler.sendMessageDelayed(message, UPDATE_DURATION);
  }

  protected final class UpdateHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
        case UPDATE_MESSAGE:
          updateWidget();
          break;
        default:
          break;
      }
    }
  }
}

Service + AlarmManager 更新 Widget

上面是利用 Service 的内部消息循环更新 Widget,也可以利用 AlarmManager 来定时触发更新。在 onUpdate() 中启动 Alarm,通过 AlarmManager 来循环启动 Service,剩下的原理基本就是一样的了。记得在 onDisabled() 取消掉 Alarm。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class MyWidgetProvider extends AppWidgetProvider {
  private static final int UPDATE_DURATION = 10 * 1000; // Widget 更新间隔

  private PendingIntent pendingIntent = null;

  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntent = new Intent(getBaseContext(), WidgetService.class);
    if (pendingIntent == null) {
      pendingIntent = PendingIntent.getService(getBaseContext(), 0,
        alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
      SystemClock.elapsedRealtime(), UPDATE_DURATION, pendingIntent);
  }

  @Override
  public void onDisabled(Context context) {
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    manager.cancel(pendingIntent);
  }
}

MyService 源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class MyService extends Service
{
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    buildUpdate();
    return super.onStartCommand(intent, flags, startId);
  }

  private void buildUpdate() {
    RemoteViews view = new RemoteViews(getPackageName(), R.layout.widget);

    RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetManager.updateAppWidget(new ComponentName(context, MyWidgetProvider.class), remoteViews);
  }
}

添加自定义 View

更新 Widget 是通过 RemoteViews 实现的,而 RemoteViews 支持的 View 有限,详细参考这里,如果想要在 Widget 中使用自定义 View,可以通过一下方式实现:

1
2
3
4
5
6
7
8
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

MyCustomView customView = new MyCustomView(context);
customView.measure(width, height);
customView.layout(0, 0, width, height);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
customView.draw(new Canvas(bitmap));
remoteViews.setImageViewBitmap(R.id.bitmap, bitmap);

实际上就是将自定义 View 在 Bitmap 上绘制,然后通过 ImageView 进行展现。

处理点击事件

RemoteViews 可以设置 setOnClickPendingIntent,通过 PendingIntent 来处理点击事件:

1
2
3
4
5
6
7
8
9
// 设置 button 事件为启动一个 Activity
Intent intent1 = new Intent("open_widget_activity");
PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, intent1, 0);
remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent1);

// 设置 button 事件为发送一个广播
Intent intent2 = new Intent("send_broadcast");
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, 0);
remoteViews.setOnClickPendingIntent(R.id.button2, pendingIntent2);

然后需要处理事件的 Activity 或者 Receiver 接受对应的 Intent 即可。

Nov 12th2015 4:14 pm  android,widget


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值