Android 提示用户安装APK
我正在为我的Android应用程序建立自己的更新机制(我没有使用Play Store)。我可以使用Android 提示用户安装APK,android,android-intent,apk,android-10.0,viewaction,Android,Android Intent,Apk,Android 10.0,Viewaction,我正在为我的Android应用程序建立自己的更新机制(我没有使用Play Store)。我可以使用DownloadManager成功下载新的apk。下载完成后,我想提示用户安装apk。我尝试过以下方法: File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk"); String test = file.getA
DownloadManager
成功下载新的apk。下载完成后,我想提示用户安装apk。我尝试过以下方法:
File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");
String test = file.getAbsolutePath();
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("content://" + file.getAbsolutePath()),
"application/vnd.android.package-archive");
context.startActivity(promptInstall);
File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW, fileUri);
promptInstall.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
promptInstall.setDataAndType(fileUri, "application/vnd.android.package-archive");
promptInstall.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(promptInstall);
不幸的是,这根本不起作用。文件已存在,但没有安装提示。出什么事了?我是否还必须将第二个参数调整为setDataAndType
?它应该在SDK 23到29上工作
编辑:
我现在尝试了以下方法:
File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");
String test = file.getAbsolutePath();
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("content://" + file.getAbsolutePath()),
"application/vnd.android.package-archive");
context.startActivity(promptInstall);
File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW, fileUri);
promptInstall.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
promptInstall.setDataAndType(fileUri, "application/vnd.android.package-archive");
promptInstall.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(promptInstall);
File File=context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY\u DOWNLOADS+“/app debug.apk”);
urifileuri=Uri.fromFile(文件)//对于Build.VERSION.SDK(INT=24){
fileUri=FileProvider.getUriForFile(上下文,BuildConfig.APPLICATION_ID+“.provider”,文件);
}
Intent promptInstall=新的Intent(Intent.ACTION\u视图,fileUri);
PrompInstall.putExtra(Intent.EXTRA\u非未知\u来源,true);
setDataAndType(fileUri,“application/vnd.android.package归档”);
prompInstall.setFlags(意图、标志、活动、新任务);
addFlags(Intent.FLAG\u GRANT\u READ\u URI\u权限);
上下文。启动触觉(PrompInstall);
在清单中,我现在有以下内容:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="ir.greencode"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/path" />
</provider>
我已经创建了path.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." /></paths>
但现在我得到了错误信息:
java.lang.IllegalArgumentException:找不到的元数据
具有权限的提供程序com.testapp.provider
我下载的APK位于/storage/simulated/0/Android/data/com.testapp/files/Download/app debug.APK
。这意味着文件
指向该目录。最后,我希望将apk存储在内部存储器或SD存储器上(取决于可用空间的位置)
如何解决此问题?可能重复的
我认为这条线索的答案应该有效: