日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 启动相同 id 的第二个前台 Service 关闭通知 )

發布時間:2025/6/17 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 启动相同 id 的第二个前台 Service 关闭通知 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、 前臺 Service 通知問題
  • 二、 設置 startForeground id 參數為 0
  • 三、 啟動相同 id 的第二個前臺 Service 關閉通知
    • 1、 前臺服務 1
    • 2、 關閉通知欄的服務
    • 3、清單文件
  • 四、源碼資源





一、 前臺 Service 通知問題



上一篇博客 【Android 進程保活】提升進程優先級 ( 使用前臺 Service 提高應用進程優先級 | 效果展示 | 源碼資源 ) 實現了一個前臺 Service , 在通知欄 , 存在一個通知 ;





二、 設置 startForeground id 參數為 0



在開啟 Service 時 , 調用的 startForeground(0, notification) 方法中 , 傳入的第一個參數 id 如果設置為 0 , 此時就不會彈出通知欄 , 但是同樣 , 進程會變成后臺進程 ;

啟動后沒有通知 ,

按下 Home 鍵后查詢 , 發現該應用就變成了普通后臺應用 , 沒有進程提權的效果 ;





三、 啟動相同 id 的第二個前臺 Service 關閉通知



不同版本的前臺服務策略 :

  • API Level < 18 : 直接使用 startForeground(10, new Notification()) 代碼啟動即可 ;
startForeground(10, new Notification());
  • API Level 18 ~ 25 : 直接使用 startForeground(10, new Notification()) 代碼啟動 , 但是必須啟動兩個前臺服務進程 , 綁定相同的 id , 后一個服務開啟后馬上關閉 , 即可將通知欄移除 ;
startForeground(10, new Notification());// API 18 ~ 25 以上的設備 , 啟動相同 id 的前臺服務 , 并關閉 , 可以關閉通知startService(new Intent(this, CancelNotificationService.class)); public class CancelNotificationService extends Service {public CancelNotificationService() {}@Overridepublic void onCreate() {super.onCreate();startForeground(10, new Notification());stopSelf();}@Overridepublic IBinder onBind(Intent intent) {return null;}}
  • API Level >= 26 : ① 無法關閉通知欄 ; ② 必須手動創建通知通道 , 以及完整參數的通知 ;
// 創建通知通道NotificationChannel channel = new NotificationChannel("service","service", NotificationManager.IMPORTANCE_NONE);channel.setLightColor(Color.BLUE);channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 正式創建service.createNotificationChannel(channel);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service");Notification notification = builder.setOngoing(true).setSmallIcon(R.mipmap.ic_launcher).setPriority(PRIORITY_MIN).setCategory(Notification.CATEGORY_SERVICE).build();// 開啟前臺進程 , API 26 以上無法關閉通知欄startForeground(10, notification);

1、 前臺服務 1


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();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){// startForeground();// 創建通知通道NotificationChannel channel = new NotificationChannel("service","service", NotificationManager.IMPORTANCE_NONE);channel.setLightColor(Color.BLUE);channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 正式創建service.createNotificationChannel(channel);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service");Notification notification = builder.setOngoing(true).setSmallIcon(R.mipmap.ic_launcher).setPriority(PRIORITY_MIN).setCategory(Notification.CATEGORY_SERVICE).build();// 開啟前臺進程 , API 26 以上無法關閉通知欄startForeground(10, notification);} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){startForeground(10, new Notification());// API 18 ~ 25 以上的設備 , 啟動相同 id 的前臺服務 , 并關閉 , 可以關閉通知startService(new Intent(this, CancelNotificationService.class));} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2){// 將該服務轉為前臺服務// 需要設置 ID 和 通知// 設置 ID 為 0 , 就不顯示已通知了 , 但是 oom_adj 值會變成后臺進程 11// 設置 ID 為 1 , 會在通知欄顯示該前臺服務// 8.0 以上該用法報錯startForeground(10, new Notification());}}@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(10, notification);}/*** 創建通知通道* @param channelId* @param channelName* @return*/@RequiresApi(Build.VERSION_CODES.O)private String createNotificationChannel(String channelId, String channelName){// 創建通知通道NotificationChannel channel = new NotificationChannel(channelId,channelName, NotificationManager.IMPORTANCE_NONE);channel.setLightColor(Color.BLUE);channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 正式創建service.createNotificationChannel(channel);return channelId;} }

2、 關閉通知欄的服務


package kim.hsl.keep_progress_alive.foreground_service;import android.app.Notification; import android.app.Service; import android.content.Intent; import android.os.IBinder;public class CancelNotificationService extends Service {public CancelNotificationService() {}@Overridepublic void onCreate() {super.onCreate();startForeground(10, new Notification());stopSelf();}@Overridepublic IBinder onBind(Intent intent) {return null;}}

3、清單文件


<?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"/><!-- 用于提權的前臺進程, 關閉通知操作 --><serviceandroid:name=".foreground_service.CancelNotificationService"android:enabled="true"android:exported="true"/></application></manifest>



四、源碼資源



源碼資源 :

  • GitHub 地址 : https://github.com/han1202012/Keep_Progress_Alive
  • CSDN 源碼快照 : https://download.csdn.net/download/han1202012/16587038

總結

以上是生活随笔為你收集整理的【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 启动相同 id 的第二个前台 Service 关闭通知 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。