美文网首页
【Android刨坑】8.0+高版本适配

【Android刨坑】8.0+高版本适配

作者: 下雨天的小白鞋 | 来源:发表于2018-12-11 09:37 被阅读0次

    无法启动服务的问题

    报错:

    Not allowed to start service Intent : app is in background uid UidRecord{a775976 u0a204 SVC idle change:idle|uncached procs:1 seq(0,0,0)}

    .....

    解决方法:

    Intent intent =new Intent(this, ChatService.class);//启动服务

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {

    startForegroundService(intent);//解决android8.0以上无法启动服务的问题

    }else {

    startService(intent);

    }

    无法弹出通知

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    channel =new NotificationChannel("id","名字", NotificationManager.IMPORTANCE_HIGH);

    channel.enableLights(true);//设置提示灯

            channel.setLightColor(Color.RED);//设置提示灯颜色

            channel.setShowBadge(true);//显示logo

            channel.setDescription("名字");//设置描述

            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

    boolean soundEnable = PreferenceHelper.getBoolean(Constants.SET_VOICE,true);//响铃

            if (soundEnable) {

       channel.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);

            }else {

    channel.setSound(null,null);

    }

    boolean vibrateEnable = PreferenceHelper.getBoolean(Constants.SET_SHAKE,true);//震动

            channel.enableLights(true);

    if (vibrateEnable) {

    channel.enableVibration(true);

    channel.setVibrationPattern(new long[]{

    100,200,300

                });

    }else {

    channel.enableVibration(false);

    channel.setVibrationPattern(new long[]{0});

    }

    //设置锁屏可见 VISIBILITY_PUBLIC=可见

            manager.createNotificationChannel(channel);

    Notification notification =new Notification.Builder(this)

    .setChannelId(channelId)

    .setContentTitle("名字")

    .setContentText("正在运行.")

    .setWhen(System.currentTimeMillis())

    .setSmallIcon(R.mipmap.logo)

                    .build();

    startForeground(1, notification);

    图标适配

    https://blog.csdn.net/shenyo/article/details/81206964

    https://blog.csdn.net/guolin_blog/article/details/79417483

    相关文章

      网友评论

          本文标题:【Android刨坑】8.0+高版本适配

          本文链接:https://www.haomeiwen.com/subject/hefshqtx.html