android 8.0 服务启动报错Fatal Exception: java.lang.IllegalStateException Not allowed to start service Inte

Fatal Exception: java.lang.IllegalStateException
Not allowed to start service Intent { act=com.xxx.xxx.xxx pkg=com.xxx.xxx (has extras) }: app is in background uid UidRecord{1cbd9ed u0a1967 CEM idle procs:1 seq(0,0,0)}

我们需要使用startForegroundService()来代替之前大方法,但是要在Sevice中调用startForeground()方法

但是我原先的代码是api 大于18 的判断其中 ,已经使用了startForeground()(后来发现与 android 8.0 有关)

 //如果API大于18,需要弹出一个可见通知
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
            Notification.Builder builder = new Notification.Builder(this);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("KeepAppAlive");
            builder.setContentText("DaemonService is runing...");
            startForeground(NOTICE_ID,builder.build());
            // 如果觉得常驻通知栏体验不好
            // 可以通过启动CancelNoticeService,将通知移除,oom_adj值不变
            Intent intent = new Intent(this,MainActivity.class);
            startService(intent);
        }else{
            startForeground(NOTICE_ID,new Notification());
        }

 

 

现在由于android 8.0的服务我们更改为api大于26

但是这样的话可能报通知的错误

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=default defaults=0x1 flags=0x40 color=0x00000000 vis=PRIVATE)

我们更改通知,使其适配Android 8.0

使用的常量:

public static final int NOTICE_ID = 100;
private static final String CHANNEL_ONE_ID = "channel";
private static final CharSequence CHANNEL_ONE_NAME = "channelName";

我们需要更改通知

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME,
                    NotificationManager.IMPORTANCE_MIN);
            notificationChannel.enableLights(false);//如果使用中的设备支持通知灯,则说明此通知通道是否应显示灯
            notificationChannel.setShowBadge(false);//是否显示角标
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);

            NotificationManager notificationManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);

            Notification.Builder builder=new Notification.Builder(getApplicationContext(),CHANNEL_ONE_ID);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setAutoCancel(true);
            builder.setChannelId(CHANNEL_ONE_ID);
            builder.setWhen(System.currentTimeMillis());
            builder.setContentTitle("题目闹钟");
            builder.setContentText("内容闹钟");
            builder.setNumber(3);





            startForeground(NOTICE_ID, builder.build());
            // 如果觉得常驻通知栏体验不好
            // 可以通过启动CancelNoticeService,将通知移除,oom_adj值不变
            Intent intent = new Intent(this, MainActivity.class);
            startService(intent);
        } else {
            startForeground(NOTICE_ID, new Notification());
        }

 

 

参考:https://blog.csdn.net/chw12341/article/details/82291108

https://blog.csdn.net/qq_32072451/article/details/82016901

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值