当活动被破坏时设置闹钟
问题描述:
我试图在活动被破坏时设置闹钟。但它不起作用。如果我在停止活动时设置了闹钟,则它正在工作。但是对于销毁方法来说,它不起作用。我不知道我是否错过了什么或什么?如果有可能,如何在活动被破坏时设置警报?当活动被破坏时设置闹钟
以下是我的代码:
@Override
protected void onDestroy() {
databaseHandler.updatePreference(Keys.Pref.APP_CRASH_STATUS, "TRUE");
setAlarmToOpenApp();
super.onDestroy();
}
private void setAlarmToOpenApp(){
Intent alarmIntent = new Intent(ActivityDisplay.this, AppCrashReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ActivityDisplay.this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pendingIntent);
Display.log(TAG, "Alarm is set to Broadcast app crash");
}
答
的onDestroy会扭曲你的所有应用的活动,这意味着在特定的方法中的所有代码不会被执行。在onStop活动停止片刻它不会被销毁,所以除了visual之外的任何活动都会采取行动,但是使用onDestroy您永远无法做到这一点,您可以创建一个活动通知,也许这将工作。或去OS编程,但你会离开纯粹的Android,我的建议将尝试设置通知活动的报警,我不知道,但它是可能的
嗯。也许这会更好地在[Service](https://developer.android.com/training/run-background-service/index.html?hl=id)中实现,因为'onDestroy'可能无法调用。此外,你的活动被破坏,因此不可用(null)。 – yennsarah
@Amylinn当我的应用程序被销毁时可能发送广播吗? – Dhaval
[Android:检查活动是否被系统从服务中销毁]的可能重复(http://stackoverflow.com/questions/34294812/android-check-if-activity-is-destroyed-by-a-system-from - 服务) – yennsarah