计时器闲置一段时间后,将杀死Android应用? [英] A timer that will kill android app after idle for certain time?
问题描述
我的工作,将检查应用程序是否有空闲(在背景中)一定时间,并杀死应用程序,如果它是超过一定时间的服务。此外,如果用户具有带回的活性,那么它会重置计时器
问题是,如果在我的应用程序很少活动,我怎么能实现呢?我发现了一些similiar code,但如何将其调整为适合我的情况?谢谢你。
样品code:
超时类及其服务
公共类超时{
私有静态最终诠释REQUEST_ID = 0;
私有静态最后长DEFAULT_TIMEOUT = 5 * 60 * 1000; // 5分钟 私有静态的PendingIntent buildIntent(上下文CTX){
意向意图=新意图(Intents.TIMEOUT);
发件人的PendingIntent = PendingIntent.getBroadcast(CTX,REQUEST_ID,意向,PendingIntent.FLAG_CANCEL_CURRENT); 返回发送者;
} 公共静态无效的开始(上下文CTX){
ctx.startService(新意图(CTX,TimeoutService.class)); 长triggerTime = System.currentTimeMillis的()+ DEFAULT_TIMEOUT; AlarmManager AM =(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC,triggerTime,buildIntent(CTX));
} 公共静态无效的取消(上下文CTX){
AlarmManager AM =(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE); am.cancel(buildIntent(CTX)); ctx.startService(新意图(CTX,TimeoutService.class)); }}公共类TimeoutService延伸服务{
私人广播接收器mIntentReceiver; @覆盖
公共无效的onCreate(){
super.onCreate(); mIntentReceiver =新的广播接收器(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
字符串行动= intent.getAction(); 如果(action.equals(Intents.TIMEOUT)){
超时(背景);
}
}
}; IntentFilter的过滤器=新的IntentFilter();
filter.addAction(Intents.TIMEOUT);
registerReceiver(mIntentReceiver,过滤器); } 私人无效超时(上下文的背景下){
App.setShutdown(); NotificationManager纳米=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.cancelAll();
} @覆盖
公共无效的onDestroy(){
super.onDestroy(); unregisterReceiver(mIntentReceiver);
} 公共类TimeoutBinder扩展粘结剂{
公共TimeoutService的getService(){
返回TimeoutService.this;
}
} 私人最终的IBinder mBinder =新TimeoutBinder(); @覆盖
公众的IBinder onBind(意向意图){
返回mBinder;
}}
杀应用程序
android.os.Process.killProcess(android.os.Process.myPid());
您可以使用handler.postDelayed(可运行,时间),当你带回你的活动你可以称之为handler.removeCallbacks(可运行);取消postDelayed
I am working on a service that will check whether the app has idle (at the background) for certain time, and kill the app if it is exceed that given time. Also , if the user has bring back the activity, then it will reset the timer
The problem is, if there are few activities in my app , how can I achieve it ? and I found some similiar code , but how to adjust it to fit my case? Thanks.
The sample code:
timeout class and its service
public class Timeout {
private static final int REQUEST_ID = 0;
private static final long DEFAULT_TIMEOUT = 5 * 60 * 1000; // 5 minutes
private static PendingIntent buildIntent(Context ctx) {
Intent intent = new Intent(Intents.TIMEOUT);
PendingIntent sender = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
return sender;
}
public static void start(Context ctx) {
ctx.startService(new Intent(ctx, TimeoutService.class));
long triggerTime = System.currentTimeMillis() + DEFAULT_TIMEOUT;
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC, triggerTime, buildIntent(ctx));
}
public static void cancel(Context ctx) {
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
am.cancel(buildIntent(ctx));
ctx.startService(new Intent(ctx, TimeoutService.class));
}
}
public class TimeoutService extends Service {
private BroadcastReceiver mIntentReceiver;
@Override
public void onCreate() {
super.onCreate();
mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ( action.equals(Intents.TIMEOUT) ) {
timeout(context);
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intents.TIMEOUT);
registerReceiver(mIntentReceiver, filter);
}
private void timeout(Context context) {
App.setShutdown();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancelAll();
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mIntentReceiver);
}
public class TimeoutBinder extends Binder {
public TimeoutService getService() {
return TimeoutService.this;
}
}
private final IBinder mBinder = new TimeoutBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
kill app
android.os.Process.killProcess(android.os.Process.myPid());
You could use handler.postDelayed(runnable, time) and when You bring back Your activity You could call handler.removeCallbacks(runnable); to cancel postDelayed
这篇关于计时器闲置一段时间后,将杀死Android应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!