Android 8.0适配
1.非全屏透明页面不允许设置方向
解决方案:android:windowIsTranslucent设置为false

2.对隐式广播进行了限制
隐式广播大部分(少部分可以用,个推暂时没问题)被禁止,在AndroidManifest中注册的Receiver将不能够生效,如果你的清单文件中有如下的监听器:

**3.通知栏** Android 8.0 引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。用户界面将通知渠道称之为通知类别。

针对 8.0 的应用,创建通知前需要创建渠道,创建通知时需要传入 channelId,否则通知将不会显示。示例代码如下:

// 创建通知渠道

private void initNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = mContext.getString(R.string.app_name);
        NotificationChannel channel = new NotificationChannel(mChannelId, name, NotificationManager.IMPORTANCE_DEFAULT);
        mNotificationManager.createNotificationChannel(channel);
    }
}

// 创建通知传入channelId

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationBarManager.getInstance().getChannelId());

4.允许安装未知来源应用
针对 8.0 的应用需要在 AndroidManifest.xml 中声明 REQUEST_INSTALL_PACKAGES 权限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

5.后台服务限制
系统不允许后台应用创建后台服务。 因此,Android 8.0 引入了一种全新的方法,即 Context.startForegroundService(),以在前台启动新服务。

//开始定位
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //android8.0以上通过startForegroundService启动service

startForegroundService(new Intent(MainActivity.this, LocationService.class));
     } else {
     startService(new Intent(MainActivity.this, LocationService.class));
 }

6.自动升级 8.0安装需要的权限

Andriod 9.0适配
1.Apache HTTP client 相关类找不到
将 compileSdkVersion 升级到 28 之后,如果在项目中用到了 Apache HTTP client 的相关类,就会抛出找不到这些类的错误。这是因为官方已经在 Android P 的启动类加载器中将其移除,如果仍然需要使用 Apache HTTP client,可以在 Manifest 文件中加入:

2.Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉 解决方案: 在资源文件新建xml目录,新建文件 <?xml version="1.0" encoding="utf-8"?> 清单文件配置: android:networkSecurityConfig="@xml/network_security_config" 但还是建议都使用https进行传输

3.限制非Activity场景启动Activity
从Android P开始,只有当Intent flag中指定了FLAG_ACTIVITY_NEW_TASK,才允许在非Activity场景启动Activity。如果不在Intent添加FLAG_ACTIVITY_NEW_TASK,将无法通过非Activity的Context启动一个Activity,并且会抛异常。例如在一个service中启动一个Activity:

private void openFeedbackActivity(CC cc) {
        Context context = cc.getContext();
        Intent intent = new Intent(context, FeedbackActivity.class);
        if (!(context instanceof Activity)) {
            //调用方没有设置context或app间组件跳转,context为application
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        CC.sendCCResult(cc.getCallId(), CCResult.success());
    }

4.必须要授予FOREGROUND_SERVICE权限,才能够使用前台服务,否则会抛出异常。

@Override
public void onCreate() {
    super.onCreate();
    String channelID = "1";
    String channelName = "channel_name";
    NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel);
    Intent intent = new Intent(this, ForegroundActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle("前台服务测试")
            .setContentText("前台服务需要增加 FOREGROUND_SERVICE 权限")
            .setChannelId(channelID)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setContentIntent(pi)
            .build();
    startForeground(1, notification);
}

这是一个带Notification的简单前台服务, 如果我们没有在AndroidManifest中注册FOREGROUND_SERVICE权限,在Service启动的时候会抛出SecurityException异常。对此,我们只需要在AndroidManifest添加对应的权限即可,这个权限是普通权限,不需要动态申请。

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />