1. 程式人生 > >android service不被kill的方法

android service不被kill的方法

	private ActivityManager activityManager;
	activityManager = (ActivityManager) mcontext.getSystemService(Context.ACTIVITY_SERVICE);

 	IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
	filter.addAction(Intent.ACTION_SCREEN_ON);
	registerReceiver(mBatInfoReceiver, filter);

 

 
   private final 
BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() { @Override public void onReceive(final Context context, final Intent intent) { final String action = intent.getAction(); if (Intent.ACTION_SCREEN_OFF.equals(action)) { activityManager
.moveTaskToFront(Activity(你的activity)).getTaskId(), 1); } else if (Intent.ACTION_SCREEN_ON.equals(action)) { // (Activity(你的activity)).moveTaskToBack(true); } } };
這些程式碼新增在service類中。