美文网首页Android开发经验谈
Android P适配问题处理

Android P适配问题处理

作者: flywfk | 来源:发表于2019-01-08 13:59 被阅读29次
    刘海屏适配

    比如我们需要全屏显示的时候,Google在api28中已经做了处理。如下面代码:

    requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //android p 刘海屏适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {//加版本判断,28以下会报错
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode
                    = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }
    

    layoutInDisplayCutoutMode值说明:
    (1)LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT:默认情况下,全屏窗口不会使用到挖孔区域,非全屏窗口可正常使用挖孔区域。
    (2)LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS:窗口声明使用挖孔区域
    (3)LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES:窗口声明使用挖孔区域
    (4)LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER:窗口声明不使用挖孔区域

    HTTP异常

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

    <uses-library android:name="org.apache.http.legacy" android:required="false"/>
    

    CLEARTEXT communication to life.115.com not permitted by network security policy
    问题原因: Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉
    解决方案:
    在资源文件新建xml目录,新建文件network_security_config:

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true" />
    </network-security-config>
    

    清单文件配置:

    android:networkSecurityConfig="@xml/network_security_config"
    

    但还是建议都使用https进行传输

    限制非Activity场景启动Activity

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

    @Override
    public void onCreate() {
        super.onCreate();
        Intent intent = new Intent(this, MainActivity.class);
        //不加 FLAG_ACTIVITY_NEW_TASK 将抛出异常
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
    
    前台服务需要添加权限

    在安卓P版本之后,必须要授予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" />
    
    限制静态广播的接收

    升级安卓P之后,隐式广播将会被全面禁止,在AndroidManifest中注册的Receiver将不能够生效,如果你的清单文件中有如下的监听器:

    <receiver android:name="com.yanghaoyi.receiver.UpdateReceiver">
            <intent-filter>
                <action android:name="com.yanghaoyi.action.ACTION_UPDATE" />
            </intent-filter>
    </receiver>
    

    你需要移除上面的代码,并在应用中进行动态注册,例如:

    private void registerReceiver(){
        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(TOAST_ACTION);
        registerReceiver(myReceiver, intentFilter);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }
    
    非全屏透明Activity禁用设置orientation

    非全屏透明页面不允许设置方向,否则会抛Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation异常,解决方案:android:windowIsTranslucent设置为false。

    相关文章

      网友评论

        本文标题:Android P适配问题处理

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