I have downloaded apk file using download manager api and i don't know where that file is storing.now i have to find that apk file and install it Give some clue. Thanks in advance
-
have u not specified the location.– PrakharAug 20, 2013 at 9:36
-
Possible duplicate of Android: install .apk programmatically– jk2KJan 22, 2016 at 7:59
3 Answers
To install a apk use this code:
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///path/to/your.apk"))
.setType("application/vnd.android.package-archive");
startActivity(promptInstall);
To install the file automatically after download, you need to declare tell the device to perform this action after download by Broadcasting this message after download:
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
and on the handler:
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
DownloadManager dm =(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(dm.getUriForDownloadedFile(id),
dm.getMimeTypeForDownloadedFile(id));
startActivity(intent);
}
};
-
@Adb EI-Rahman, I made a changes as you said but am not getting what i have give for "filename" because i don't know where that file saving after downloaded.– UdayAug 20, 2013 at 10:54
-
@Adb EI-Rahman i put the code what you have given but it showing error "The method getUriForDownloadedFile(long) is undefined for the type DownloadManager." :(– UdayAug 21, 2013 at 4:27
-
Make sure that you put this line in the top of the class
import android.app.DownloadManager;
Aug 21, 2013 at 9:22 -
@Adb EI-Rahman yeah i have added everything. if u say yes, i ill send you the code.– UdayAug 21, 2013 at 9:40
-
This is the only problem you face now, I think you can handle it by checking the SDK you are working with or include the library manual or look for
dm.*
similar method that take the same input to return the same output. And please if you find it useful please vote the answer up Aug 21, 2013 at 9:46
@Uday, @Adb El-Rahman's code works. When you initialize the download you need to set the destination uri.
String sAndroidUrl = "http://somesite.com/Install.apk";
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(sAndroidUrl));
String sDest = "file://" + android.os.Environment.getExternalStorageDirectory().toString() + "/Download/Install.apk";
request.setDestinationUri(Uri.parse(sDest));
enqueue = dm.enqueue(request);