美文网首页
Android自更新安装APK

Android自更新安装APK

作者: Charles2018 | 来源:发表于2019-07-12 10:50 被阅读0次
fun executeInstall(cxt: Context, _file: File) {
        try{
            val DATA_AND_TYPE = "application/vnd.android.package-archive";
            val intent = Intent(Intent.ACTION_VIEW)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            val file = _file
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
                val apkUri = FileProvider.getUriForFile(cxt, BuildConfig.APPLICATION_ID +
                        ".fileProvider", file)
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                intent.setDataAndType(apkUri, DATA_AND_TYPE)
                cxt.startActivity(intent)
            } else {
                val pc:Process = Runtime.getRuntime().exec("chmod 755 $file");
                pc.waitFor()
                val apkUri = Uri.fromFile(file)
                intent.setDataAndType(apkUri, DATA_AND_TYPE)
                cxt.startActivity(intent)
            }
        }catch(e:java.lang.Exception){
            e.printStackTrace()
        }
    }

相关文章

网友评论

      本文标题:Android自更新安装APK

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