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?
Due to Android security model it is not possible to install Apk file automatically.
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:
To download a file without the browser do sth. like this:
You can temp. download it to an sd card, install it with the package manager and then remove it again.