Reputation: 689
I'm doing an app to program device lock and unlock of the screen on the specified time. To do this I'm using AlarmManager to start a service that locks or unlocks the screen. Once the service does it, it sets AlarmManager to repeat this action 24 hours after, so basically you are setting an Schedule that repeats every day.
The problem is, I want to cancel every alarmmanager that has been set if the user closes the app. I tried to do this in onDestroy event of the main activity. But it doesn't get called when I close the app.
I tried this code in the service to detect if the app is running, cancelling its action if it's not running. But this is not a correct solution because the user could have finished the app and started it again before the service run:
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningProcInfo = activityManager .getRunningAppProcesses();
for(int i = 0; i < runningProcInfo.size(); i++){
//Toast.makeText(this, runningProcInfo.get(i).processName, Toast.LENGTH_SHORT).show();
if(runningProcInfo.get(i).processName.equals("inusual.example.com.powercontrol2")) {
bAppRunning = true;
}
}
How can I implement the behaviour I want?
EDIT: this is how I set up the AlarmManager:
String sTime = (String) aTimeTextViews[i].getText();
String sHour = sTime.substring(0, sTime.indexOf(":"));
String sMinute = sTime.substring(sTime.indexOf(":") + 1, sTime.length());
int iTimeInMinutes = (Integer.parseInt(sHour)*60) + Integer.parseInt(sMinute);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sHour));
calendar.set(Calendar.MINUTE, Integer.parseInt(sMinute));
calendar.set(Calendar.SECOND, 0);
long timeInMillis = calendar.getTimeInMillis();
if (iTimeInMinutes<=currentTimeInMinutes)
{
timeInMillis += 24*60*60*1000;
}
int iOnOff = aSpinners[i].getSelectedItemPosition();
alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent;
if (iOnOff==0)
intent = new Intent(this, Serviceunlock.class);
else
intent = new Intent(this, Servicelock.class);
alarmIntent = PendingIntent.getService(this, 0, intent, 0);
//alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() ,
// AlarmManager.INTERVAL_DAY, alarmIntent);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, timeInMillis , alarmIntent);
Upvotes: 1
Views: 1241
Reputation: 689
The only way I have found that truly gets called when the app is closed (and only when the app is closed) is having a service running while my app runs, and implement my code in the onTaskRemoved event of that service.
@Override
public void onTaskRemoved(Intent rootIntent){
super.onTaskRemoved(rootIntent);
}
I discovered this on this answer: How to detect application exit on android?
Upvotes: 2
Reputation: 856
You can Write alarmMgr.cancel(pendingIntent);
on onDestroy() Method.
So When your activity is going to finish or destroying then at that time you can override the onDestroy() method and stop your alarm manager
Upvotes: 0