Android apk安装的正确姿势

作者: MonkeyLei | 来源:发表于2019-08-05 09:04 被阅读0次

没太多说的,直接上代码 - 重点是fileprovider:

//< 安装文件
public void install(String filePath)
{
    ///< 获取启动一个apk的Intent
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data;
    File file = new File(filePath);
    // 判断版本大于等于7.0
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
    {
        // "net.csdn.blog.ruancoder.fileprovider"即是在清单文件中配置的authorities
        data = FileProvider.getUriForFile(activity, "com.xxxx.fileprovider", file);
        // 给目标应用一个临时授权
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
    else
    {
        data = Uri.fromFile(file);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    intent.setDataAndType(data,"application/vnd.android.package-archive");
    activity.startActivity(intent);
}

相关文章

网友评论

    本文标题:Android apk安装的正确姿势

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