Android Auto Installation Of Apks
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 instal
Solution 1:
To download a file without the browser do sth. like this:
Stringapkurl="http://your.url.apk";
InputStream is;
try {
URLurl=newURL(apkurl);
HttpURLConnectioncon= (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);
con.connect();
is = con.getInputStream();
} catch (SSLException e) {
// HTTPS can end in SSLException "Not trusted server certificate"
}
// Path and File where to download the APKStringpath= Environment.getExternalStorageDirectory() + "/download/";
StringfileName= apkurl.substring(apkurl.lastIndexOf('/') + 1);
Filedir=newFile(path);
dir.mkdirs(); // creates the download directory if not existFileoutputFile=newFile(dir, fileName);
FileOutputStreamfos=newFileOutputStream(outputFile);
// Save file from URL to download directory on external storagebyte[] buffer = newbyte[1024];
intlen=0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
is.close();
// finally, install the downloaded file
install(path + fileName);
Solution 2:
You can temp. download it to an sd card, install it with the package manager and then remove it again.
protectedvoidinstall(String fileName) {
Intent install = newIntent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(newFile(fileName)),
"application/vnd.android.package-archive");
startActivity(install);
}
Solution 3:
Solution 4:
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:
myWebView.setWebViewClient(newWebViewClient() {
@OverridepublicvoidonReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.d("WEB_VIEW_TEST", "error code:" + errorCode + " - " + description);
}
@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
// handle different requests for different type of files// this example handles downloads requests for .apk and .mp3 files// everything else the webview can handle normallyif (url.endsWith(".apk")) {
Uri source = Uri.parse(url);
// Make a new request pointing to the .apk urlDownloadManager.Request request = newDownloadManager.Request(source);
// appears the same in Notification bar while downloading
request.setDescription("Description for the DownloadManager Bar");
request.setTitle("YourApp.apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
// save the file in the "Downloads" folder of SDCARD
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk");
// get download service and enqueue fileDownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
elseif(url.endsWith(".mp3")) {
// if the link points to an .mp3 resource do something else
}
// if there is a link to anything else than .apk or .mp3 load the URL in the webviewelse view.loadUrl(url);
returntrue;
}
});
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
privateBroadcastReceiveronDownloadComplete=newBroadcastReceiver() {
@OverridepublicvoidonReceive(Context context, Intent intent) {
Stringaction= intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
// toast here - download complete
}
}
};
remember to recister recevier in the main activity like this:
registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Post a Comment for "Android Auto Installation Of Apks"