Android 如何最大限度的?;詈笈_進程

前言:本文將通過兩種方式,在手機亮屏和屏鎖狀態(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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容