Android 8.0サービス起動エラーFatal Exception:java.lang.IllegalStateException Not allowed to start service Inte

3396 ワード

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