Android DownloadManager - Download apk and install it

久未见 提交于 2019-12-10 12:04:45

问题


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));

回答1:


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);



回答2:


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()


来源:https://stackoverflow.com/questions/29050067/android-downloadmanager-download-apk-and-install-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!