版本适配

版本适配

这里列举了部分需要开发中进行适配的功能变更

Android 6.0

运行时权限:用户可直接在运行时管理应用权限

Android 7.0

多窗口支持

FileProvider

功能适配

Android8.0 允许安装未知来源权限

public void onInstall(View view) {
    AndPermission.with(this)
                 .runtime()
                 .permission(Permission.Group.STORAGE)
                 .onGranted(new Action<List<String>>() {
                     @Override
                     public void onAction(List<String> data) {
                         installApp();
                     }
                 })
                 .onDenied(new Action<List<String>>() {
                     @Override
                     public void onAction(List<String> data) {
                     }
                 }).start();
}

private void installApp() {
    File file = new File(Environment.getExternalStorageDirectory(), "0/1.apk");
    if(!file.exists()) {
        Toast.makeText(InstallAppActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
        return;
    }

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        boolean yes = getPackageManager().canRequestPackageInstalls();
        if(!yes) {
            Uri packageURI = Uri.parse("package:" + getPackageName());
            Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
            startActivityForResult(intent, INSTALL_PERMISS_CODE);            } else {
            realInstallApk(file);
        }
    } else {
        realInstallApk(file);
    }
}

private void realInstallApk(File file) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        String authority = getPackageName() + ".fileprovider";
        Uri contentUri = FileProvider
                .getUriForFile(getApplicationContext(), authority, file);
        intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
    } else {
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
    }
    startActivity(intent);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if(resultCode == RESULT_OK && requestCode == INSTALL_PERMISS_CODE) {
        realInstallApk(new File(Environment.getExternalStorageDirectory(), "0/1.apk"));
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Last updated

Was this helpful?