java - 从 Instant App 下载并安装 apk(无需在 Playstore 中发布)

标签 java android android-intent apk android-instant-apps

我已将 .apk 文件上传到保管箱。我想在用户单击即时应用程序中的安装按钮时下载并启动应用程序。但调用startActivity()后出现以下异常:

Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{8593d11 2399:com.google.android.packageinstaller/u0a26} (pid=2399, uid=10026) that is not exported from uid 10298

 

Intent intent = new Intent(Intent.ACTION_VIEW);
if (intent.resolveActivity(getPackageManager()) == null) {
    Log.e("No Packate Manager", intent.getPackage() + "");
    return;
}

Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  //BuildConfig.APPLICATION_ID
    // File file = new File(File folder, "app-debug.apk");
    //uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".fileprovider", file);
    uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".fileprovider", new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "app-debug.apk"));
    List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
} else {
    // uri = Uri.fromFile(new File("/mnt/sdcard/Download/app-debug.apk"));
    File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File outputFile = new File(folder, "app-debug.apk");
    uri = Uri.fromFile(outputFile);
}

intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// install.setDataAndType(myuri, manager.getMimeTypeForDownloadedFile(downloadId));

startActivity(intent);

最佳答案

无法从免安装应用启动 Play 商店外部托管的 APK 文件的安装(也不建议这样做)。

您收到的错误是因为即时应用程序无法读取(或写入)外部磁盘存储。有关即时应用程序中受限功能的更多信息,请参阅docs 。尽管即时应用可以读取/写入自己的内部存储,但它们无法将该数据公开或共享给其他应用。

建议的方法是将常规 APK 发布到 Google Play,然后使用提供的 showInstallPrompt()启动安装的帮助程序方法。这会将用户引导至 Google Play 来安装您的应用。

您还应该查看我们的 UX guidelines用于呈现这样的安装按钮(称为“显式安装提示”)。

关于java - 从 Instant App 下载并安装 apk(无需在 Playstore 中发布),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44382549/

相关文章:

java - 具有 READ COMMITTED 隔离级别的 SQL 并发 'select then update' 模式

java - 每个 webapp 一个或多个 servlet?

android - 当我们从 Android 的外部应用程序手动更改权限时会发生什么

android - 如何获取已安装媒体播放器的列表

java - org.jvnet.jaxb2.maven2 :maven-jaxb2-plugin : same file , 倍数结果

java - 如何更改 Struts2 中的默认日期格式?

java - MediaRecorder : IOexception prepare failed error . 请帮助我

android - CoordinatorLayout 使 View 未定位

Android 构建 - "duplicate class: com.myapp.R"

java - Android Mediaplayer无法使用路径/documents/audio:1159加载Uri