我正在使用AlarmManager定期启动服务,以从服务器检索数据:
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, Service.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, 0);
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
pendingIntent);
所以它启动了,一切都很顺利。。。
唯一的问题是我不知道如何从中更新主UI…我看到的所有例子都没有使用AlarmManager。。。所以它们不符合我的需求。
你能帮帮我吗?
谢谢!
您的服务将无法访问UI线程。但是可以使用代码中引用的上下文对象来调用
runOnUiThread(Runnable Action);
如果你的家人有Runnable和其他阅读能力,那么你就可以达到我的目标。
否则,
让您的服务启动Intent广播并创建一个扩展BroadcastReceiver 的嵌套类
private class updateUIReceiver extends BroadcastReceiver{
在当前包含UI线程的"活动"中。然后使用发挥创意
Intent.putExtra();
告诉您的活动如何更新UI。例如字符串是int。常量运行良好。