android force kill schedule service every minute

那年仲夏 提交于 2019-12-07 15:00:36

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12974605/android-force-kill-schedule-service-every-minute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!