android 安装apk对不同版本的要求

android手机越往后,对权限的要求越来越高!其中安装apk就是这样.

以下说下在android7.0手机上安装apk的具体操作:
第一步:在res资源文件下,新建xml文件夹,xml文件夹下命名file_paths资源文件,其写法如下:

name 可以自定义

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="download"
        path="."/>
</paths>

第二步:在androidMainfest.xml文件中配置
注意:1,authorities的配置必须与项目中FileProvider.getUriForFile()所填写参数一致
2,resource的配置与在res下新建的file_paths保持一致

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.liangzi.demo.sdkdemo.fileProvider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

第三步:安装apk

public class InstallUtil {

    //安装
    public static void installNormal(Context context, String apkPath) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        //版本在7.0以上是不能直接通过uri访问的
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            File file = new File(apkPath);
            Log.e("apkPath", apkPath);
            Uri apkUri = FileProvider.getUriForFile(context, "com.liangzi.demo.sdkdemo.fileProvider", file);
            // 由于没有在Activity环境下启动Activity,设置下面的标签
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(new File(apkPath)),
                    "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }
}

同时需要注意:在安装调用InstallUtils.installNormal()前,我们需要提升读写权限操作:调用SystemManager.setPermission(String filePaht)

public class SystemManager {
    /**
     * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限)
     * @param command 命令:String apkRoot="chmod 777 "+getPackageCodePath();
     * @return  0 命令执行成功
     */
    public static int RootCommand(String command) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            int i = process.waitFor();
            Log.d("SystemManager", "i:" + i);
            return i;
        } catch (Exception e) {
            Log.d("SystemManager", e.getMessage());
            return -1;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 提升读写权限
     * @param filePath 文件路径
     * @return
     * @throws IOException
     */
    public static void setPermission(String filePath)  {
        String command = "chmod " + "777" + " " + filePath;
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

版权声明:本文为willba原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。