首页 > 解决方案 > 更新 targetSdkVersion 28(非市场)中的 APK 问题

问题描述

APK 在 targetSdkVersion 24之前更新良好,但在28(latest)中存在问题。我的应用不在 PlayStore 上。如果版本号发生更改,它需要更新。问题是当版本号在 28 targetSdkVersion 中增加时,App 没有更新这是我的代码:

try {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/Download/"+appName);
        file.setReadable(true, false);
        Uri fileUri = Uri.fromFile(file);
        if (Build.VERSION.SDK_INT >= 24) {
            fileUri = FileProvider.getUriForFile(UpdateActivity.this, getPackageName()+ ".provider",
                    file);
        }
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE, fileUri);
        intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
        intent.setDataAndType(fileUri, "application/vnd.android" + ".package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
        finish();
    } catch (Exception e) {
        Toast.makeText(this, "Something went Wrong.Remove old XYZ app & install it from Download folder", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

在清单中:

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

提供者路径:

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

我需要它来更新 targetSdkVersion 28 中的 APK。感谢帮助谢谢

标签: androidupdatesauto-updatetarget-sdktargetsdkversion

解决方案


推荐阅读