【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 效果展示 | 源码资源 )
生活随笔
收集整理的這篇文章主要介紹了
【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 效果展示 | 源码资源 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、 使用前臺 Service 提高應用進程優先級
- 1、 前臺 Service 代碼
- 2、 前臺 Service 代碼
- 3、 啟動服務
- 二、效果展示
- 三、源碼資源
一、 使用前臺 Service 提高應用進程優先級
上一篇博客 【Android 進程保活】提升進程優先級 ( 1 像素 Activity 提高進程優先級 | taskAffinity 親和性說明 | 運行效果 | 源碼資源 ) 使用了前臺 Activity , 提升整個進程的優先級 ;
前臺進程中除了前臺顯示的 Activity 之外 , 還有前臺服務 , 即調用 startForeground 方法啟動的服務 ;
按下 Home 鍵后 , 通過前臺服務 , 讓后臺進程仍然是前臺進程 ;
1、 前臺 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();// 將該服務轉為前臺服務// 需要設置 ID 和 通知// 設置 ID 為 0 , 就不顯示已通知了 , 但是 oom_adj 值會變成后臺進程 11// 設置 ID 為 1 , 會在通知欄顯示該前臺服務//startForeground(1, new Notification());startForeground();}@Overridepublic IBinder onBind(Intent intent) {return null;}/*** 啟動前臺服務*/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);}/*** 創建通知通道* @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、 前臺 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><!--設置最近任務列表中不顯示該 Activity 組件 ( 不要被用戶察覺 )android:excludeFromRecents="true"設置 Activity 親和性讓該界面在一個獨立的任務棧中 , 不要與本應用的其它任務棧放在一起避免解除鎖屏后 , 關閉 1 像素界面 , 將整個任務棧都喚醒android: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" /><!-- 用于提權的前臺進程 --><serviceandroid:name=".foreground_service.ForegroundService"android:enabled="true"android:exported="true"/></application></manifest>
3、 啟動服務
=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 提升應用權限// 注冊廣播接收者 , 1 像素 Activity 啟動的 廣播接收者//KeepProgressAliveManager.getmInstance().registerReceiver(this);// 通過前臺 Service 提升應用權限// 啟動普通 Service , 但是在該 Service 的 onCreate 方法中執行了 startForeground// 變成了前臺 Service 服務startService(new Intent(this, ForegroundService.class));}@Overrideprotected void onDestroy() {super.onDestroy();// 取消注冊廣播接收者, 也可以不取消注冊//KeepProgressAliveManager.getmInstance().registerReceiver(this);} }
二、效果展示
啟動應用 ,
查看 Logcat 中的 PID 為 300143001430014 ,
查詢進程 oom_adj 值 , 000 , 前臺進程 ;
點擊 Home 鍵 ,
查詢 oom_adj 值 , 444 , 在 【Android 進程保活】oom_adj 值 ( oom_adj 值對應的進程優先級 | oom_adj 值動態改變 | 進程保活優化方向 ) 可以看到該進程是后臺重量級進程 , 比后臺進程 999 ~ 151515 優先級高 ;
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
總結
以上是生活随笔為你收集整理的【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 效果展示 | 源码资源 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【错误记录】前台进程报错 ( Bad n
- 下一篇: 【Android 进程保活】提升进程优先