2

Hi guys was wondering if there was a bit of code I could use that would make an app auto install once the download completes?

My app has a download section with in it. I was using Google Drive to handle the downloads. but I am encountering issues with some devices. So I have decided to move away from google

I am now using media fire as my host. My app uses direct download. But it always downloads using the download manager. What I would like it to do is more like how Google Drive works with direct download. Which is it gives me the option to install as soon as download completes.which i have now solved with these few lines of code

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new 
File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), 
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

is there a way to check download folder before downloading the file. if the file is already there install if not got to web page for download. rather it saying parse error then going to webpage or having multiple downloads of same file.

Thanks in advance as always.

P9p studios
  • 69
  • 1
  • 8
  • 1
    You can refer http://stackoverflow.com/questions/4967669/android-install-apk-programmatically – Sandeep Bhandari Mar 07 '16 at 11:31
  • brilliant thanks for that would i need to write that for every download or could implement it so as to write it once but use it over and over for each download. – P9p studios Mar 07 '16 at 11:40
  • 1
    Write it in a method which accepts a parameter like url of the place where your apk is getting downloaded or if its a fixed path then just pass the name of apk to that method :) Every time your download is completed call the method with apk name of file path :) I believe the same code should work fine for each download :) – Sandeep Bhandari Mar 07 '16 at 12:02
  • brill thanks sandeep# – P9p studios Mar 07 '16 at 14:08
  • ok got my head round that bit but came across a new problem edited post with explanation – P9p studios Mar 08 '16 at 02:03

1 Answers1

1

You can get the downloaded Uri after the download complete, so you don't have to specify a file name to save. If you use DownloadManager, below is a simple example.

    final DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://remotehost/your.apk"));
    final long id = downloadManager.enqueue(request);
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                Intent installIntent = new Intent(Intent.ACTION_VIEW);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    installIntent.setDataAndType(downloadManager.getUriForDownloadedFile(id),
                            "application/vnd.android.package-archive");
                } else {
                    Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(id));
                    try {
                        if (cursor != null && cursor.moveToFirst()) {
                            int status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
                            String localUri = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI));
                            if (status == DownloadManager.STATUS_SUCCESSFUL) {
                                installIntent.setDataAndType(Uri.parse(localUri), "application/vnd.android.package-archive");
                            }
                        }
                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                    }
                }
                installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.sendBroadcast(installIntent);
            }
        }
    };
    registerReceiver(broadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
alijandro
  • 11,627
  • 2
  • 58
  • 74
  • also is there away to delete apk on request. so lets say ive installed it and dont wnat ot keep the file.. but so i have choice. – P9p studios Mar 08 '16 at 11:49
  • @MarkMinecrafterHarrop You can use download id to track multiple downloads. Of course you can delete the file after installation done. You can get the path of downloaded file from `DownloadManager`, then delete it. – alijandro Mar 09 '16 at 01:46