android service不被kill的方法
阿新 • • 發佈:2018-11-10
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這些程式碼新增在service類中。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); } } };