Android 自动安装 APK
我有一个 webview,它基本上能够拦截各种链接、视频、apk、href。
现在,我想要的是,一旦我从网址下载 APK,它将自动安装:
这是 shouldOverrideUrlLoading()
代码的一部分:
else if(url.endsWith(".apk"))
{
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(final String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
}
});
Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));
startActivity(intent);
return true;
如果我添加
intent.setDataAndType(Uri.parse(url), "application/vnd.android.package-archive");
,应用程序就会崩溃...
关于做什么有什么想法吗?
编辑:我能够自动启动包的下载和安装(使用 sleep() ):
else if(url.endsWith(".apk"))
{
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(final String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
}
});
Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));
startActivity(intent);
String fileName = Environment.getExternalStorageDirectory() + "/download/" + url.substring( url.lastIndexOf('/')+1, url.length() );
install(fileName);
return true;
并且,正如 vitamoe 建议的那样:
protected void install(String fileName) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(fileName)),
"application/vnd.android.package-archive");
startActivity(install);
}
但是,我无法捕获下载完成的确切时间,可能需要创建我自己的下载功能而不使用浏览器的下载功能,有什么想法吗?
I have a webview which basically is capable of intercepting all sorts of links, video, apks, hrefs.
Now, what I want is once I download an APK from a url, that it'll be auto installed:
This is part of the shouldOverrideUrlLoading()
code:
else if(url.endsWith(".apk"))
{
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(final String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
}
});
Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));
startActivity(intent);
return true;
If I add
intent.setDataAndType(Uri.parse(url), "application/vnd.android.package-archive");
Than the application crashes...
Any ideas as to what to do?
EDIT: I was able to initiate a download and an installation of the package automatically (using a sleep() ):
else if(url.endsWith(".apk"))
{
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(final String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
}
});
Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));
startActivity(intent);
String fileName = Environment.getExternalStorageDirectory() + "/download/" + url.substring( url.lastIndexOf('/')+1, url.length() );
install(fileName);
return true;
and, as vitamoe suggested:
protected void install(String fileName) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(fileName)),
"application/vnd.android.package-archive");
startActivity(install);
}
However, I'm unable to capture the exact time that the download is finished, might need to create my own download function and not use the browser's one, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要在没有浏览器的情况下下载文件,请执行以下操作。像这样:
To download a file without the browser do sth. like this:
你可以临时。将其下载到 SD 卡,使用包管理器安装,然后再次删除。
You can temp. download it to an sd card, install it with the package manager and then remove it again.
由于 Android 安全模型,无法自动安装 Apk 文件。
Due to Android security model it is not possible to install Apk file automatically.
为什么不尝试使用下载管理器和在下载完成时拦截的广播接收器?下载管理器适用于 ANDROID 2.3+,
示例如下:
完整答案:用户 bboydflo
将文件下载到Android WebView(代码中没有下载事件或 HTTPClient)
下载完成时拦截的广播接收器
记住在主活动中注册接收器,如下所示:
why not trying with a download manage and a broadcast receiver that would intercept when download is finished? Download manager works for ANDROID 2.3+ though
Example here:
Full answer here: user bboydflo
Downloading a file to Android WebView (without the download event or HTTPClient in the code)
Broadcast receiver to intercept when download has finished
remember to recister recevier in the main activity like this: