前言:本文將通過兩種方式,在手機亮屏和屏鎖狀態(tài)下聯(lián)合對進程進行?;?。
一、手機亮屏時如何提高存活率
在點擊home鍵使app長時間停留在后臺時,內存不足時會被殺死。
處理這種情況時運用灰色?;睿趕ervice里通過Service.startForeground() 設置為前臺服務,提高存活率。
if (Build.VERSION.SDK_INT < 18) {
//Android4.3以下 ,隱藏Notification上的圖標
startForeground(GRAY_SERVICE_ID, new Notification());
} else if (Build.VERSION.SDK_INT > 18 && Build.VERSION.SDK_INT < 25) {
//Android4.3 - Android7.0,隱藏Notification上的圖標
Intent innerIntent = new Intent(this, GrayInnerService.class);
startService(innerIntent);
startForeground(GRAY_SERVICE_ID, new Notification());
} else {
//7.0以上暫時沒有辦法隱藏Notification上的圖標 且需要先注冊渠道
String channelId = "my_service";
String channelName = "前臺service";
int importance = NotificationManager.IMPORTANCE_DEFAULT;//優(yōu)先級設置為最高
createNotificationChannel(channelId, channelName, importance);
Notification notification = new NotificationCompat.Builder(this, "my_service")
.build();
startForeground(GRAY_SERVICE_ID, notification);
}
GrayInnerService
/**
* 灰色?;?用于4.3-7.0系統(tǒng)取消通知欄圖標
* */
public static class GrayInnerService extends Service {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(GRAY_SERVICE_ID, new Notification());
stopForeground(true);
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
二、手機鎖屏時如何提高存活率
手機在進入鎖屏狀態(tài)一段時間,省電機制會殺死后臺進程。
處理這種情況時,我們需注冊廣播監(jiān)聽鎖屏和解鎖事件, 鎖屏后啟動一個1像素的透明Activity, 解鎖后銷毀這個透明Activity。
注:這個廣播要寫到要?;畹膕ervice中。
//注冊鎖屏解鎖廣播
mOnePixelReceiver = new OnePixelReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mOnePixelReceiver, filter);
/**
* 廣播監(jiān)聽,手機鎖屏時啟動1像素Activity
* */
public class OnePixelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//屏幕關閉啟動1像素Activity
Intent it = new Intent(context, OnePiexlActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//屏幕打開 發(fā)送通知驗證沒死
sendNotification();
}
}
}
OnePiexlActivity.class
*
* 透明 Activity 用戶不察覺
**/
public class OnePiexlActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設置1像素
Window window = getWindow();
window.setGravity(Gravity.LEFT | Gravity.TOP);
WindowManager.LayoutParams params = window.getAttributes();
params.x = 0;
params.y = 0;
params.height = 1;
params.width = 1;
window.setAttributes(params);
//檢查屏幕狀態(tài)
checkScreen();
}
@Override
protected void onResume() {
super.onResume();
checkScreen();
}
/**
* 檢查屏幕狀態(tài) isScreenOn為true 屏幕“亮”結束該Activity
*/
private void checkScreen() {
PowerManager pm = (PowerManager) OnePiexlActivity.this.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if (isScreenOn) {
finish();
}
}
}
總結:以上就是在手機鎖屏和亮屏時對進程進行?;畹膬煞N方法
源碼地址:https://download.csdn.net/download/u014085912/11912451