I want to schedule a service to run every minute and check if my app is still running. (I want to reopen the application if it is closed). Also, I still want this service to run every minute if my application was force killed by task manager. Thanks!
-
don't. just don't. don't force your app open, that's the best way to piss users and have bad reviews. (unless you have a _very_ good reason, which i'm curious to read) – njzk2 Oct 19 '12 at 13:04
-
im writing a "Parent Control" app which is installed on the child's phone. I want to prevent him from force killing the app unless it is closed by the parent. – Erel Hansav Oct 19 '12 at 13:10
-
every minute give a 1 minute time frame to do stuff without the app knowing ... – njzk2 Oct 22 '12 at 09:34
2 Answers
Also, I still want this service to run every minute if my application was force killed by task manager
This is not possible as of Android 3.1. If the user goes into Settings and force=stops your app, nothing of your app will run again, until the user manually launches one of your components.
If your process is terminated for other reasons (e.g., ordinary task-killer app from the Play Store, swiping your task away from the Recent Tasks list), your alarms scheduled with AlarmManager
should remain intact, per Lucifer's suggestion.
im writing a "Parent Control" app which is installed on the child's phone.
Any child sufficiently intelligent to use a phone will be sufficiently intelligent to reboot their device in safe mode and get rid of your app.

- 986,068
- 189
- 2,389
- 2,491
-
What you are saying is that if i go into the built-in task manager and press "Kill" (Like in ice-cream sandwitch) it will still run every minute, but if he goes into Settings and Force Stop's the app it wont run every minute, did i understand you correctly? thanks! – Erel Hansav Oct 19 '12 at 13:35
-
@ErelHansav: "if he goes into Settings and Force Stop's the app it wont run every minute" -- correct. There is no built-in task manager in Android, other than the Recent Tasks list, which (last I checked) does not show the word "kill" anywhere. – CommonsWare Oct 19 '12 at 22:45
-
In my ICE CREAM SANDWICH android, press HOME button for long time, it shows me my recent apps, and on the bottom it shows "Task Manager" button. When i click it i go into task manager where i can see my open applications, RAM, memory.... there i can press the "Kill" button. thats what i mean ... thanks! – Erel Hansav Oct 20 '12 at 11:55
-
@ErelHansav: That is something unique to your device. It is not part of Android, but part of something your device manufacturer added to Android. – CommonsWare Oct 20 '12 at 11:59
-
Use AlarmManager
class, it works even if your device is in sleep mode.
private static Intent alarmIntent = null;
private static PendingIntent pendingIntent = null;
private static AlarmManager alarmManager = null;
// First Creating an Intent
alarmIntent = new Intent ( context, yourClass.class );
// Create an Pending Intent which will Broadcast the Intent
pendingIntent = PendingIntent.getBroadcast(context, 234324243, alarmIntent, 0 );
// Set the AlarmManager class
alarmManager = ( AlarmManager ) context.getSystemService( ConstantCodes.ALARM_SERVICE );
// Set Repeating time interval
alarmManager.setRepeating( AlarmManager.RTC_WAKEUP, Interval * 1000, Interval * 1000, pendingIntent );
AlarmManager consumes lesser battery power than TimerTask or Thread. It works like painless AsyncTask.

- 29,392
- 25
- 90
- 143
-
I read in here: [link](http://stackoverflow.com/questions/9101818/how-to-create-a-persistent-alarmmanager) that it cannot be done. What is different between what you suggested and what they are doing ? Thanks! – Erel Hansav Oct 19 '12 at 13:12
-
-
What is ConstantCodes.ALARM_SERVICE? Did you mean Activity.ALARM_SERVICE ? – Erel Hansav Oct 19 '12 at 15:28
-
-
In order to stop it from another class, do i need to do this: alarmIntent = new Intent ( context, ChildCheckService.class ); pendingIntent = PendingIntent.getBroadcast(context, 234324243, alarmIntent, 0 ); AlarmManager.cancel(pendingIntent) ? Or is these another way ? Thanks – Erel Hansav Oct 19 '12 at 15:37
-