0

I am trying to set an alarm when activity is destroyed. But it is not working. If I set an alarm when activity is stopped, it is working. But for the destroying method, it is not working. I don't know if I am missing something or what ? If it is possible,how can I set an alarm when activity is destroyed?

Following is my code:

@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");
    }
Dhaval
  • 2,724
  • 2
  • 24
  • 33
  • 1
    Hmm. Maybe this would be better to implement in a [Service](https://developer.android.com/training/run-background-service/index.html?hl=id), as `onDestroy` is possible to not get called. Also, your Activity is destroyed and therefor not available (null). – yennsarah Jun 07 '16 at 10:02
  • @Amylinn Is it possible send broadcast when my app is being destroyed? – Dhaval Jun 07 '16 at 10:06
  • Possible duplicate of [Android: Check if activity is destroyed by a system from service](http://stackoverflow.com/questions/34294812/android-check-if-activity-is-destroyed-by-a-system-from-service) – yennsarah Jun 07 '16 at 10:15
  • @Amylinn Yes we can check activity status by that method. But I was trying to set alarm. – Dhaval Jun 07 '16 at 10:22
  • It's not only about the state. Service has it's own context, you could use that instead? – yennsarah Jun 07 '16 at 10:25
  • @Amylinn I will try to implement using service. – Dhaval Jun 07 '16 at 10:26

1 Answers1

-1

onDestroy would distort all your app activity which means all the code in the specific method will not be performed. in onStop activity stops for a moment it isn't destroyed so any activity except visual will take in action, however with onDestroy you will never be able to do that, you can create an activity for notifications and maybe that'll work out. Or go for OS programming but you would move away from pure android, my suggestion would be try to set that alarm on the notification activity, I'm not sure but it is possible

Kuantew
  • 134
  • 6