2016-06-07 48 views
0

我試圖在活動被破壞時設置鬧鐘。但它不起作用。如果我在停止活動時設置了鬧鐘,則它正在工作。但是對於銷燬方法來說,它不起作用。我不知道我是否錯過了什麼或什麼?如果有可能,如何在活動被破壞時設置警報?當活動被破壞時設置鬧鐘

以下是我的代碼:

@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"); 
    } 
+1

嗯。也許這會更好地在[Service](https://developer.android.com/training/run-background-service/index.html?hl=id)中實現,因爲'onDestroy'可能無法調用。此外,你的活動被破壞,因此不可用(null)。 – yennsarah

+0

@Amylinn當我的應用程序被銷燬時可能發送廣播嗎? – Dhaval

+0

[Android:檢查活動是否被系統從服務中銷燬]的可能重複(http://stackoverflow.com/questions/34294812/android-check-if-activity-is-destroyed-by-a-system-from - 服務) – yennsarah

回答

-1

的onDestroy會扭曲你的所有應用的活動,這意味着在特定的方法中的所有代碼不會被執行。在onStop活動停止片刻它不會被銷燬,所以除了visual之外的任何活動都會採取行動,但是使用onDestroy您永遠無法做到這一點,您可以創建一個活動通知,也許這將工作。或去OS編程,但你會離開純粹的Android,我的建議將嘗試設置通知活動的報警,我不知道,但它是可能的

相關問題