【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )
生活随笔
收集整理的這篇文章主要介紹了
【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、報錯信息
- 二、解決方案
一、報錯信息
使用如下代碼啟動前臺服務 :
public class ForegroundService extends Service {public ForegroundService() {}@Overridepublic void onCreate() {super.onCreate();// 將該服務轉為前臺服務// 需要設置 ID 和 通知// 設置 ID 為 0 , 就不顯示已通知了 , 但是 oom_adj 值會變成后臺進程 11// 設置 ID 為 1 , 會在通知欄顯示該前臺服務startForeground(1, new Notification());}@Overridepublic IBinder onBind(Intent intent) {return null;} }Android 8.0 以下沒問題 , 8.0 及以上 , 報如下錯誤 ;
報錯信息 :
2021-04-08 19:36:34.736 23830-23830/? E/AndroidRuntime: FATAL EXCEPTION: mainProcess: kim.hsl.keep_progress_alive, PID: 23830android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1745)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:193)at android.app.ActivityThread.main(ActivityThread.java:6718)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)二、解決方案
Android 8.0 以上不能用空的通知了 , 必須自己創建通知通道 , 創建通知 ;
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;} }總結
以上是生活随笔為你收集整理的【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【错误记录】前台进程报错 ( Permi
- 下一篇: 【Android 进程保活】提升进程优先