从另一个程序安装 apk
我正在尝试创建一个应用程序,自动从特定服务器下载 apk 并将其安装在系统上。我的安装代码如下所示,但不起作用。
File f = new File("/mnt/sdcard/download/", "Demo.apk");
Log.i("Demo", "f "+f.getAbsoluteFile());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package_archive");
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
m_context.startActivity(intent);
我需要在 Manifest.xml
中授予安装权限吗?我知道这个问题以前曾被问过,但到目前为止,没有一个答案对我有帮助。
I'm trying to create an application that automatically downloads an apk from a specific server and install it on the system. My code for the installation looks like the following, but does not work.
File f = new File("/mnt/sdcard/download/", "Demo.apk");
Log.i("Demo", "f "+f.getAbsoluteFile());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package_archive");
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
m_context.startActivity(intent);
Do i need to give any rights in Manifest.xml
for installation? I know that question has been asked before, but none of the answers have helped me so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是我在我的例子中所做的,
这些是权限..
This what I do in my case,
And these are the permissions..
感谢大家的帮助,终于成功了。我分享我的工作代码和工作 Manifest.xml。
清单.xml
//弗雷德里克
Thanks for all help, made it work at last. I share my working code and working Manifest.xml.
Manifext.xml
//Fredrik
我也这样做
,我的安装看起来像这样,
我的
path
是一个字符串,就像你的 fi also do
and my install lookd like this
my
path
is a String, like your f你需要这样做
在你的代码中,你提到了“package_archive”,它应该是“package-archive”。
您需要以下权限。
You need to do this
In your code, you have mentioned "package_archive", it should be "package-archive".
You would need the following permissions.
如果您的
targetSdkVersion
等于或高于 24,那么您需要为 Android N 及更高版本的 Android 版本使用FileProvider
实现。整个实现如下:
将provider
FileProvider
添加到AndroidManifest
中,并在xml资源下创建
provider_paths.xml
。有关 FileProvider 配置的更多信息,您可以阅读此处 和此处。
所有积分均归 @just_user 因为我的答案基于 他的回复。
If your
targetSdkVersion
is equal or higher than 24, then you need to useFileProvider
implementation for Android N and newer Android versions.Here is the whole implementation:
Add provider
FileProvider
toAndroidManifest
And create
provider_paths.xml
under xml resources.More info about FileProvider configuration you can read here and here.
All credits go to @just_user because my answer based on his reply.