android force kill schedule service every minute

2019-05-14 04:19发布

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!

2条回答
我只想做你的唯一
2楼-- · 2019-05-14 05:06

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-05-14 05:09

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.

查看更多
登录 后发表回答