5

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

Uday
  • 171
  • 1
  • 4
  • 12

3 Answers3

6

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); 
Prakhar
  • 2,270
  • 19
  • 26
3

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);
}
};
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • @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. – Uday Aug 20 '13 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." :( – Uday Aug 21 '13 at 04:27
  • Make sure that you put this line in the top of the class `import android.app.DownloadManager;` – Sami Eltamawy Aug 21 '13 at 09:22
  • @Adb EI-Rahman yeah i have added everything. if u say yes, i ill send you the code. – Uday Aug 21 '13 at 09: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 – Sami Eltamawy Aug 21 '13 at 09:46
0

@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);  
Adrian Olar
  • 2,883
  • 4
  • 35
  • 63
Dave S
  • 849
  • 7
  • 7