简体   繁体   中英

Android DownloadManager - Download apk and install it

I have looked everywhere to find out how to download a APK that is not from the market place. The APK is stored in my server. All the sample did not work.

I want my App to download an APK for my server and promt the user to install it.

So far I managed to download the apk onto the user sd. but I can't get he path where is it saved and get to install it.

private void downloadAPK() {
    String url="https://www.example.com/app.apk”;
    downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    request = new Request(Uri.parse(url));
    request.setTitle(“My App“);
    enqueue = downloadManager.enqueue(request);


      new Thread(new Runnable() {

        @Override
        public void run() {

            boolean downloading = true;

            while (downloading) {

                try {
                    Thread.sleep(10000);
                     DownloadManager.Query q = new DownloadManager.Query();
                        q.setFilterById(enqueue);

                        Cursor cursor = downloadManager.query(q);
                        cursor.moveToFirst();
                        int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                        int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                        if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                            downloading = false;
                        }

                        final int dl_progress = (int) ((double)bytes_downloaded / (double)bytes_total * 100f);

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {

                                Log.d("Prog", Integer.toString(dl_progress));
                            }
                        });


                        cursor.close();
                } catch (InterruptedException e) {

                }

            }

        }
    }).start();
}}


BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();


        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
             Log.w(“”, “Downloaded”)

        }
        else {
            //do something here
       }

    }
};


onCreate ...
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

According to the documentation you can specify the location where you want the file to be saved. All you have to do is adding this line before setting the request to de DownloadManager :

request.setDestinationInExternalFilesDir (context, dirType, subPath);

You can Query about your download local file using the code below :

Note that : will return null if the download failed.

String path = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).getPath()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM