这应该是适配安卓8.0最好的亲身列子了。。。。

随着市面上8.0的安卓手机日渐增多,我们公司app也要适配到安卓8.0。在适配的过程中,也发现了很多问题,也特此记录一下!

第一个,权限问题。因为我公司的app的 TargetSdkVersion的版本一直是22,(为什么是22,我接手后就是22)所以也没有加入权限适配。所以这一次,我就把公司的TargetSdkVersion的版本改成27,其他什么也都改成27,所以首先解决的就是权限问题。权限问题还是比较好解决,在Gitbub上找了好几个权限适配库,发现还是AndPermission这个权限库比较适合我,而且这个作者也一直在更新!主要是这个库代码风格我比较喜欢。权限问题没什么好说,都是运行中给出权限即可,app需要什么权限,你给它就可以了。这种链式代码风格你喜欢吗?

  private void requestPermission(String... permissions) {
        AndPermission.with(this)
                .runtime()
                .permission(permissions)
                .rationale(new RuntimeRationale())
                .onGranted(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> permissions) {
                        Bundle bundle = new Bundle();
                        bundle.putBoolean("AddDevice", true);
                        openActivity(ScanByZBarActivity.class, bundle);
                    }
                })
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(@NonNull List<String> permissions) {
                        if (AndPermission.hasAlwaysDeniedPermission(getActivity(), permissions)) {
                            showSettingDialog(getActivity(), permissions);
                        }
                    }
                })
                .start();
    }

第二个:在适配的过程中,发现打开app放到哪里,过了一会app就会无缘无故闪退。当然,出现问题,AS肯定是有日志,所以根据日志定位到了StartService()这个代码上。查了资料才明白,安卓8.0之后,系统是不允许后台应用创建后台服务, 因此,Android 8.0 引入了一种全新的方法,即 Context.startForegroundService(),在前台启动新服务。 在系统创建服务后,应用有5秒的时间来调用该服务的 startForeground() 方法以显示新服务的用户可见通知。如果应用在此时间限制内未调用 startForeground(),则系统将停止服务并声明此应用为 ANR。所以这也是为什么app会无缘无故出现闪退的原因。好,知道了这个原因,直接来适配:

Intent intent = new Intent(context, MainService.class);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }

这里是做了一个判断,当sdk版本>26的时候,就创建一个前台服务,所以app的通知栏会出现一个通知,app在运行中。所以也间接了告诉客户,我这个app在运行中!


第三个问题:通知栏。这次通知栏其实也没有大改,就是加了一个ChanneID,用来允许要为显示的每种通知类型创建用户可自定义的渠道,用户界面将通知渠道称之为通知类别。第一步:获取通知服务 NotificationManager。

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(Config.CHANNEL_ID, “随便填点啥”, NotificationManager.IMPORTANCE_HIGH);
            mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotifyManager.createNotificationChannel(channel);
        } else {
            mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }

代码也很简单,也就是判断一下版本。然后获取实列了。

第二步;

  NotificationCompat.Builder builder = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                builder = new NotificationCompat.Builder(context, Config.CHANNEL_ID);
            } else {
                builder = new NotificationCompat.Builder(context);
            }
            builder.setSmallIcon(R.mipmap.logo_notice);
            builder.setContentTitle(getString(R.string.app_name));
            builder.setAutoCancel(true);

在这里就是多了一个

    builder = new NotificationCompat.Builder(context, Config.CHANNEL_ID);

这个Channerl_id,可以随便填写。然后其他就是和之前的通知栏就是一样了。



第三个:也是适配安卓7.0的出现的问题。发现app在线升级的时候,升级app没有问题,但是升级之后,打开apk安装发现闪退了。后面查了资料:

Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file:// URI。如果一项包含文件 URI 的 intent 离开您的应用,则应用出现故障,并出现 FileUriExposedException 异常。

要在应用间共享文件,您应发送一项 content:// URI,并授予 URI 临时访问权限。也就是说,对于应用间共享文件这块,Android N中做了强制性要求

<!--加入此权限 8.0系统才能安装apk成功-->
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
 <provider
            android:name=".utils.MyFileProvider"
            android:authorities="你的包名.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_path"
                tools:replace="name,resource"/>
        </provider>
这里为什么是 
  android:name=".utils.MyFileProvider"
而不是:
 android:name="android.support.v4.content.FileProvider"

后面我在来解释。

然后还需要生成一个xml/file_path 文件

<?xml version="1.0" encoding="utf-8"?>
<paths>

    <external-path
        name="files_root"
        path="Android/data/你的包名/"/>
    <external-path
        name="external_storage_root"
        path="."/>
</paths>

打开apk的代码

public static void installApk(Context context, File fileName) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, "你的包名.fileprovider", fileName);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(fileName), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }
好了,解决上一个问题。
android:name=".utils.MyFileProvider"

因为我的项目中集成了

compile 'com.jph.takephoto:takephoto_library:4.0.3'

如果大家集成了这个项目,就知道了,因为这个项目本身就适配安卓7.0获取应用外部公开 file:// URI,也就是多了,也就多了个FileProvider,所以有冲突,然后还是查了资料,自己重新建了一个类。

public class MyFileProvider extends FileProvider {

}

这样就不会冲突了,然后验证一下,升级app之后,果然没有打开apk闪退的原因啦!然后获取相册中的图片设置头像也没有问题了。

好了,这次app适配安卓8.0就到此结束了,其实都还好,在解决的过程中,虽然发现了很多问题,但是后面都慢慢解决了!大家也将自己的app适配到8.0吧! 如果大家在适配的过程有什么问题,也都可以给我留言。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值