应用关闭时接收 TIME_SET 广播
Receiving TIME_SET Broadcast when app is closed
我想在用户更改 android 系统时间时在首选项中存储一个布尔值。因此,我将广播操作 ACTION_TIME_CHANGED 添加到清单中:
<receiver android:name="test.TimeChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
TimeChangedReceiver 扩展了 BroadcastReceiver 并覆盖了 onReceive()。在此 class 中,将存储布尔值并显示通知。
public class TimeChangedReceiver extends BroadcastReceiver {
private static final String TAG = "TimeChangedReceiver";
public TimeChangedReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
// store boolean to SharedPreferences
PreferenceUtils.getInstance().storeBoolean(true, PreferenceUtils.CHECK_LICENSE);
// build notification
int icon = android.R.drawable.ic_dialog_alert;
String title = "changed time";
String text = "user changed time";
long when = System.currentTimeMillis();
String timezone = intent.getStringExtra("time-zone");
Notification notification = new Notification(icon, title, when);
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notification_id = (int) System.currentTimeMillis();
Intent notificationIntent = new Intent(context, MainView.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra("time-zone", timezone);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT );
notification.setLatestEventInfo(context, title, text, contentIntent);
mgr.notify(notification_id, notification);
}
}
应用程序关闭之前一切正常 - 不再在后台运行。
这里说:
- If you declare the BroadcastReceiver in the Manifest, it will always be active and be called even if the application is closed/stopped
- By putting your BroadcastReceiver in your Manifest, it, by default, is always active.
我怎样才能接收广播并存储布尔值?
我不需要看通知。
来自docs:
A process that is currently executing a BroadcastReceiver (that is,
currently running the code in its onReceive(Context, Intent) method)
is considered to be a foreground process and will be kept running by
the system except under cases of extreme memory pressure.
然后
This means that for longer-running operations you will often use a
Service in conjunction with a BroadcastReceiver to keep the containing
process active for the entire time of your operation.
所以我想你需要实现一个注册接收者的服务,以确保接收者的进程具有高优先级,从而保持 运行.
Everything is working fine until the app is closed - not runnning in the background anymore.
There are no logs when I "force stop" the application and changing the date afterwards.
您的观察是正确的,被认为是正常行为。通过系统设置执行 "force stop" 后,您的 BroadcastReceiver
将不再收到消息,即使您在 AndroidManifest.xml 中声明了它们。通过执行 "force stop",您基本上是在指示系统:"stop this app now and don't let it run again"。这是一项管理操作,因此只能通过 系统设置 访问。这是有道理的不是吗?新安装的应用程序也是如此。在至少启动一次之前,它不会收到任何广播意图。
这就是 Android 系统的设计方式。你无能为力。
How can I receive the broadcast anyway and store the boolean?
在 "force stop" 之后,您的应用程序(或其组件之一)必须再次由用户手动(重新)启动至少一次。在这种情况下,即使在系统完全重新启动后,在您的 AndroidManifest.xml 中声明的接收器也会按预期接收广播意图。
您不应将 "force stop" 与系统破坏您的应用程序的情况(例如,由于内存不足)混淆。这些是不一样的,因为 "force stop" 为您的应用程序设置了一个永久标志,以防止它再次 运行。
我想在用户更改 android 系统时间时在首选项中存储一个布尔值。因此,我将广播操作 ACTION_TIME_CHANGED 添加到清单中:
<receiver android:name="test.TimeChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
TimeChangedReceiver 扩展了 BroadcastReceiver 并覆盖了 onReceive()。在此 class 中,将存储布尔值并显示通知。
public class TimeChangedReceiver extends BroadcastReceiver {
private static final String TAG = "TimeChangedReceiver";
public TimeChangedReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
// store boolean to SharedPreferences
PreferenceUtils.getInstance().storeBoolean(true, PreferenceUtils.CHECK_LICENSE);
// build notification
int icon = android.R.drawable.ic_dialog_alert;
String title = "changed time";
String text = "user changed time";
long when = System.currentTimeMillis();
String timezone = intent.getStringExtra("time-zone");
Notification notification = new Notification(icon, title, when);
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notification_id = (int) System.currentTimeMillis();
Intent notificationIntent = new Intent(context, MainView.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra("time-zone", timezone);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT );
notification.setLatestEventInfo(context, title, text, contentIntent);
mgr.notify(notification_id, notification);
}
}
应用程序关闭之前一切正常 - 不再在后台运行。
这里说:
- If you declare the BroadcastReceiver in the Manifest, it will always be active and be called even if the application is closed/stopped
- By putting your BroadcastReceiver in your Manifest, it, by default, is always active.
我怎样才能接收广播并存储布尔值?
我不需要看通知。
来自docs:
A process that is currently executing a BroadcastReceiver (that is, currently running the code in its onReceive(Context, Intent) method) is considered to be a foreground process and will be kept running by the system except under cases of extreme memory pressure.
然后
This means that for longer-running operations you will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation.
所以我想你需要实现一个注册接收者的服务,以确保接收者的进程具有高优先级,从而保持 运行.
Everything is working fine until the app is closed - not runnning in the background anymore.
There are no logs when I "force stop" the application and changing the date afterwards.
您的观察是正确的,被认为是正常行为。通过系统设置执行 "force stop" 后,您的 BroadcastReceiver
将不再收到消息,即使您在 AndroidManifest.xml 中声明了它们。通过执行 "force stop",您基本上是在指示系统:"stop this app now and don't let it run again"。这是一项管理操作,因此只能通过 系统设置 访问。这是有道理的不是吗?新安装的应用程序也是如此。在至少启动一次之前,它不会收到任何广播意图。
这就是 Android 系统的设计方式。你无能为力。
How can I receive the broadcast anyway and store the boolean?
在 "force stop" 之后,您的应用程序(或其组件之一)必须再次由用户手动(重新)启动至少一次。在这种情况下,即使在系统完全重新启动后,在您的 AndroidManifest.xml 中声明的接收器也会按预期接收广播意图。
您不应将 "force stop" 与系统破坏您的应用程序的情况(例如,由于内存不足)混淆。这些是不一样的,因为 "force stop" 为您的应用程序设置了一个永久标志,以防止它再次 运行。