app版本更新 下载apk后没有跳转到安装页面

app版本更新 下载apk后没有跳转到安装页面
如果遇到这种情况,可以试试下面的两种方法
1.添加请求安装app的权限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

2.authority不对。请检查AndroidManifest.xml中配置的authorities是否与引用时一致。

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.administrator.omplatformengineer.provider"
            android:exported="false"
            tools:replace="android:authorities"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />

        </provider>

以下是引用时的代码

Intent intent =new Intent(Intent.ACTION_VIEW);
                File apkFile = queryDownloadedApk(downManager,downloadId);
                //判断是否是AndroidN以及更高的版本
                if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
                    Uri contentUri = FileProvider.getUriForFile(context,
                            "com.example.administrator.omplatformengineer.provider",apkFile);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.setDataAndType(contentUri,"application/vnd.android.package-archive");
                }else{
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setDataAndType(Uri.fromFile(apkFile),"application/vnd.android.package-archive");
                }
                context.startActivity(intent);

authorities="com.example.administrator.omplatformengineer.provider这里要一致

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android Studio 中实现应用程序版本更新,一般可以通过以下步骤进行: 1. 在应用程序的后端服务器上创建一个文件,用于存放最新版本apk 文件的下载链接以及版本号等信息。 2. 在应用程序中添加一个检查更新的功能,当用户打开应用程序时,通过与后端服务器交互,检查当前版本是否为最新版本。 3. 如果不是最新版本,则提示用户更新。此时可以采用两种方式: ① 弹出对话框提示用户更新,如果用户点击“确定”按钮,则跳转下载最新版 apk 文件的页面,让用户下载安装最新版的应用程序。 ② 直接下载最新版 apk 文件,并提示用户安装。此方法需要先在 AndroidManifest.xml 文件中添加下载权限。 以下是一些参考代码: ```java // 检查应用程序是否有新版本 private void checkUpdate() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://your-backend-server.com/version.json").build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { // 请求失败 } @Override public void onResponse(Call call, Response response) throws IOException { String json = response.body().string(); // 解析 json 文件,获取最新版本号以及 apk 文件下载链接等信息 if (needUpdate) { // 提示用户更新 runOnUiThread(new Runnable() { @Override public void run() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("发现新版本"); builder.setMessage("是否立即更新?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 跳转下载最新版 apk 文件的页面 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(apkUrl)); startActivity(intent); } }); builder.setNegativeButton("取消", null); builder.show(); } }); } } }); } ``` 如果选择直接下载最新版 apk 文件的方式,可以参考以下代码: ```java private void downloadApk() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(apkUrl).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { // 下载失败 } @Override public void onResponse(Call call, Response response) throws IOException { InputStream inputStream = response.body().byteStream(); FileOutputStream fos = null; try { File file = new File(getExternalFilesDir(null), "app.apk"); fos = new FileOutputStream(file); byte[] buffer = new byte[2048]; int len; while ((len = inputStream.read(buffer)) != -1) { fos.write(buffer, 0, len); } fos.flush(); // 安装应用程序 installApk(file); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { inputStream.close(); } if (fos != null) { fos.close(); } } } }); } private void installApk(File file) { Intent intent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri uriForFile = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", file); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(uriForFile, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } ``` 以上代码仅供参考,具体实现方式还需根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值