关于targetSDKVersion=26适配8.0以上的一些坑

最近犹豫项目需要,需要把targetSDKVersion提升到26,原来以为只是一句代码搞定的事情,后来经测试发现的两个坑:

一:系统弹窗(悬浮窗)不生效

项目里使用了SYSTEM_ALERT_WINDOW     权限弹了在service里面设置了dialog的TYPE_SYSTEM_ALERT的
悬浮窗,发现不好用。解决办法:

如果应用使用 SYSTEM_ALERT_WINDOW 权限并且尝试使用以下窗口类型之一来在其他应用和系统窗口上方显示提醒窗口:

  • TYPE_PHONE
  • TYPE_PRIORITY_PHONE
  • TYPE_SYSTEM_ALERT
  • TYPE_SYSTEM_OVERLAY
  • TYPE_SYSTEM_ERROR
请用 TYPE_APPLICATION_OVERLAY  替换
dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));

二:通知不生效

项目里用到了通知提醒,提升后8.0的机器上也不好用了。查阅API发现,原来的

new NotificationCompat.Builder(this)升到8.0后被弃用了,改为了  new NotificationCompat.Builder(this,String channelId)
而channelId的设置如下:

String id = "channel_1";
String description = "123";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, "123", importance);
  mChannel.setDescription(description);
  mChannel.enableLights(true);
  mChannel.setLightColor(Color.BLUE);
  mChannel.enableVibration(true);
  mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(mChannel);
如果不生成channel,直接填入一个id会不生效的。

所以升到26的同学适配8.0机型的时候一定要注意这两个问题,要

if(Build.VERSION.SDK_INT>=26){}else{}
判断分别设置的。








  





你可能感兴趣的:(android)