【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 效果展示 | 源码资源 )
文章目錄
- 一、 使用前臺(tái) Service 提高應(yīng)用進(jìn)程優(yōu)先級(jí)
- 1、 前臺(tái) Service 代碼
- 2、 前臺(tái) Service 代碼
- 3、 啟動(dòng)服務(wù)
- 二、效果展示
- 三、源碼資源
一、 使用前臺(tái) Service 提高應(yīng)用進(jìn)程優(yōu)先級(jí)
上一篇博客 【Android 進(jìn)程保活】提升進(jìn)程優(yōu)先級(jí) ( 1 像素 Activity 提高進(jìn)程優(yōu)先級(jí) | taskAffinity 親和性說(shuō)明 | 運(yùn)行效果 | 源碼資源 ) 使用了前臺(tái) Activity , 提升整個(gè)進(jìn)程的優(yōu)先級(jí) ;
前臺(tái)進(jìn)程中除了前臺(tái)顯示的 Activity 之外 , 還有前臺(tái)服務(wù) , 即調(diào)用 startForeground 方法啟動(dòng)的服務(wù) ;
按下 Home 鍵后 , 通過(guò)前臺(tái)服務(wù) , 讓后臺(tái)進(jìn)程仍然是前臺(tái)進(jìn)程 ;
1、 前臺(tái) Service 代碼
package kim.hsl.keep_progress_alive.foreground_service;import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Build; import android.os.IBinder;import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat;import kim.hsl.keep_progress_alive.R;import static androidx.core.app.NotificationCompat.PRIORITY_MIN;public class ForegroundService extends Service {public ForegroundService() {}@Overridepublic void onCreate() {super.onCreate();// 將該服務(wù)轉(zhuǎn)為前臺(tái)服務(wù)// 需要設(shè)置 ID 和 通知// 設(shè)置 ID 為 0 , 就不顯示已通知了 , 但是 oom_adj 值會(huì)變成后臺(tái)進(jìn)程 11// 設(shè)置 ID 為 1 , 會(huì)在通知欄顯示該前臺(tái)服務(wù)//startForeground(1, new Notification());startForeground();}@Overridepublic IBinder onBind(Intent intent) {return null;}/*** 啟動(dòng)前臺(tái)服務(wù)*/private void startForeground() {String channelId = null;// 8.0 以上需要特殊處理if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {channelId = createNotificationChannel("kim.hsl", "ForegroundService");} else {channelId = "";}NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);Notification notification = builder.setOngoing(true).setSmallIcon(R.mipmap.ic_launcher).setPriority(PRIORITY_MIN).setCategory(Notification.CATEGORY_SERVICE).build();startForeground(1, notification);}/*** 創(chuàng)建通知通道* @param channelId* @param channelName* @return*/@RequiresApi(Build.VERSION_CODES.O)private String createNotificationChannel(String channelId, String channelName){NotificationChannel chan = new NotificationChannel(channelId,channelName, NotificationManager.IMPORTANCE_NONE);chan.setLightColor(Color.BLUE);chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);service.createNotificationChannel(chan);return channelId;} }
2、 前臺(tái) Service 代碼
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="kim.hsl.keep_progress_alive"><uses-permission android:name="android.permission.FOREGROUND_SERVICE" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.Keep_Progress_Alive"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!--設(shè)置最近任務(wù)列表中不顯示該 Activity 組件 ( 不要被用戶察覺(jué) )android:excludeFromRecents="true"設(shè)置 Activity 親和性讓該界面在一個(gè)獨(dú)立的任務(wù)棧中 , 不要與本應(yīng)用的其它任務(wù)棧放在一起避免解除鎖屏后 , 關(guān)閉 1 像素界面 , 將整個(gè)任務(wù)棧都喚醒a(bǔ)ndroid:taskAffinity="kim.hsl.keep_progress_alive.alive"--><activityandroid:name=".one_pixel_activity.OnePixelActivity"android:excludeFromRecents="true"android:taskAffinity="kim.hsl.keep_progress_alive.onepixel"android:theme="@style/OnePixelActivityTheme" /><!-- 用于提權(quán)的前臺(tái)進(jìn)程 --><serviceandroid:name=".foreground_service.ForegroundService"android:enabled="true"android:exported="true"/></application></manifest>
3、 啟動(dòng)服務(wù)
=package kim.hsl.keep_progress_alive;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent; import android.os.Bundle;import kim.hsl.keep_progress_alive.foreground_service.ForegroundService; import kim.hsl.keep_progress_alive.one_pixel_activity.KeepProgressAliveManager;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 1 像素 Activity 提升應(yīng)用權(quán)限// 注冊(cè)廣播接收者 , 1 像素 Activity 啟動(dòng)的 廣播接收者//KeepProgressAliveManager.getmInstance().registerReceiver(this);// 通過(guò)前臺(tái) Service 提升應(yīng)用權(quán)限// 啟動(dòng)普通 Service , 但是在該 Service 的 onCreate 方法中執(zhí)行了 startForeground// 變成了前臺(tái) Service 服務(wù)startService(new Intent(this, ForegroundService.class));}@Overrideprotected void onDestroy() {super.onDestroy();// 取消注冊(cè)廣播接收者, 也可以不取消注冊(cè)//KeepProgressAliveManager.getmInstance().registerReceiver(this);} }
二、效果展示
啟動(dòng)應(yīng)用 ,
查看 Logcat 中的 PID 為 300143001430014 ,
查詢進(jìn)程 oom_adj 值 , 000 , 前臺(tái)進(jìn)程 ;
點(diǎn)擊 Home 鍵 ,
查詢 oom_adj 值 , 444 , 在 【Android 進(jìn)程保活】oom_adj 值 ( oom_adj 值對(duì)應(yīng)的進(jìn)程優(yōu)先級(jí) | oom_adj 值動(dòng)態(tài)改變 | 進(jìn)程保活優(yōu)化方向 ) 可以看到該進(jìn)程是后臺(tái)重量級(jí)進(jìn)程 , 比后臺(tái)進(jìn)程 999 ~ 151515 優(yōu)先級(jí)高 ;
C:\Users\octop>adb shell walleye:/ $ su walleye:/ # cat /proc/30014/oom_adj 0 walleye:/ # cat /proc/30014/oom_adj 4 walleye:/ #三、源碼資源
源碼資源 :
- GitHub 地址 : https://github.com/han1202012/Keep_Progress_Alive
- CSDN 源碼快照 : https://download.csdn.net/download/han1202012/16581773
總結(jié)
以上是生活随笔為你收集整理的【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 效果展示 | 源码资源 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【错误记录】前台进程报错 ( Bad n
- 下一篇: 【Android 进程保活】提升进程优先