【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 启动相同 id 的第二个前台 Service 关闭通知 )
生活随笔
收集整理的這篇文章主要介紹了
【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 启动相同 id 的第二个前台 Service 关闭通知 )
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 一、 前臺 Service 通知問題
- 二、 設(shè)置 startForeground id 參數(shù)為 0
- 三、 啟動相同 id 的第二個(gè)前臺 Service 關(guān)閉通知
- 1、 前臺服務(wù) 1
- 2、 關(guān)閉通知欄的服務(wù)
- 3、清單文件
- 四、源碼資源
一、 前臺 Service 通知問題
上一篇博客 【Android 進(jìn)程保活】提升進(jìn)程優(yōu)先級 ( 使用前臺 Service 提高應(yīng)用進(jìn)程優(yōu)先級 | 效果展示 | 源碼資源 ) 實(shí)現(xiàn)了一個(gè)前臺 Service , 在通知欄 , 存在一個(gè)通知 ;
二、 設(shè)置 startForeground id 參數(shù)為 0
在開啟 Service 時(shí) , 調(diào)用的 startForeground(0, notification) 方法中 , 傳入的第一個(gè)參數(shù) id 如果設(shè)置為 0 , 此時(shí)就不會彈出通知欄 , 但是同樣 , 進(jìn)程會變成后臺進(jìn)程 ;
啟動后沒有通知 ,
按下 Home 鍵后查詢 , 發(fā)現(xiàn)該應(yīng)用就變成了普通后臺應(yīng)用 , 沒有進(jìn)程提權(quán)的效果 ;
三、 啟動相同 id 的第二個(gè)前臺 Service 關(guān)閉通知
不同版本的前臺服務(wù)策略 :
- API Level < 18 : 直接使用 startForeground(10, new Notification()) 代碼啟動即可 ;
- API Level 18 ~ 25 : 直接使用 startForeground(10, new Notification()) 代碼啟動 , 但是必須啟動兩個(gè)前臺服務(wù)進(jìn)程 , 綁定相同的 id , 后一個(gè)服務(wù)開啟后馬上關(guān)閉 , 即可將通知欄移除 ;
- API Level >= 26 : ① 無法關(guān)閉通知欄 ; ② 必須手動創(chuàng)建通知通道 , 以及完整參數(shù)的通知 ;
1、 前臺服務(wù) 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();// 創(chuàng)建通知通道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);// 正式創(chuàng)建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();// 開啟前臺進(jìn)程 , API 26 以上無法關(guān)閉通知欄startForeground(10, notification);} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){startForeground(10, new Notification());// API 18 ~ 25 以上的設(shè)備 , 啟動相同 id 的前臺服務(wù) , 并關(guān)閉 , 可以關(guān)閉通知startService(new Intent(this, CancelNotificationService.class));} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2){// 將該服務(wù)轉(zhuǎn)為前臺服務(wù)// 需要設(shè)置 ID 和 通知// 設(shè)置 ID 為 0 , 就不顯示已通知了 , 但是 oom_adj 值會變成后臺進(jìn)程 11// 設(shè)置 ID 為 1 , 會在通知欄顯示該前臺服務(wù)// 8.0 以上該用法報(bào)錯(cuò)startForeground(10, new Notification());}}@Overridepublic IBinder onBind(Intent intent) {return null;}/*** 啟動前臺服務(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(10, notification);}/*** 創(chuàng)建通知通道* @param channelId* @param channelName* @return*/@RequiresApi(Build.VERSION_CODES.O)private String createNotificationChannel(String channelId, String channelName){// 創(chuàng)建通知通道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);// 正式創(chuàng)建service.createNotificationChannel(channel);return channelId;} }
2、 關(guān)閉通知欄的服務(wù)
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><!--設(shè)置最近任務(wù)列表中不顯示該 Activity 組件 ( 不要被用戶察覺 )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)的前臺進(jìn)程 --><serviceandroid:name=".foreground_service.ForegroundService"android:enabled="true"android:exported="true"/><!-- 用于提權(quán)的前臺進(jìn)程, 關(guān)閉通知操作 --><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
總結(jié)
以上是生活随笔為你收集整理的【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 启动相同 id 的第二个前台 Service 关闭通知 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android 进程保活】提升进程优先
- 下一篇: 【错误记录】BLE 蓝牙搜索失效 ( 关