简体   繁体   English

下载后使用下载管理器安装apk并退出应用程序

[英]Install apk after download with download manager and exit from app

i have created an android app and from server if any new version had released it will automatically start downloading by using inbuilt 'download manager' . 我创建了一个Android应用程序,并从服务器,如果任何新版本已发布它将自动开始下载使用内置的“下载管理器”。 for auto install after finish the download i have created a broadcast receiver to inform that download has finished and completed and then i start to install it. 完成下载后自动安装我创建了一个广播接收器,通知下载已完成并完成,然后我开始安装它。 it works fine during i stay in app and don't close it. 它在我留在应用程序期间工作正常,不要关闭它。 but my problem is when i close the application . 但我的问题是当我关闭应用程序。 so after finishing the download i want to automatically install it. 所以在完成下载后我想自动安装它。 but i wont happen. 但我不会发生。 what should i do for this issue? 我该怎么办这个问题?

    private void download(String link) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
    request.setDescription("new version");
    request.setTitle("app name");
    request.setMimeType("application/vnd.android.package-archive");
    request.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, "myapk.apk");
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

    downloadID = dm.enqueue(request);


    br = new BroadcastReceiver() {

        @Override
        public void onReceive(Context c, Intent i) {
            String action = i.getAction();

            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(downloadID);

                Cursor downloadResult = dm.query(query);

                if (downloadResult.moveToFirst()) {
                    int statusColumnIndex = downloadResult.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    int status = downloadResult.getInt(statusColumnIndex);

                    if (status == DownloadManager.STATUS_SUCCESSFUL) {
                        //download completed successfully
                        int localFileNameId = downloadResult.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);

                        String downloadPathFile = downloadResult.getString(localFileNameId);
                        String downloadPathDir = downloadPathFile.substring(0, downloadPathFile.lastIndexOf("/") + 1);
                        String downloadName = downloadPathFile.substring(downloadPathFile.lastIndexOf("/") + 1);

                        Log.i("name =", downloadName);

                        File file = new File(downloadPathDir);
                        File[] files = file.listFiles();
                        for (File f : files) {
                            if (f.isFile() && f.exists() && !f.getName().equals(downloadName)) {
                                f.delete();
                            }
                        }

                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(downloadPathFile)), "application/vnd.android.package-archive");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        unregisterReceiver(br);
                    }
                }
            }
        }
    };

    registerReceiver(br, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

}

You could do something like this: 你可以这样做:

in your manifest 在你的清单中

<receiver android:name="packageName.DownloadReceiver" android:exported="true"> 
    <intent-filter> 
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/> 
    </intent-filter> 
</receiver>

and then in DownloadReceiver.java add the logic to handle auto install for example 然后在DownloadReceiver.java添加处理自动安装的逻辑

public class DownloadReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            // add your logic here
        }
    }
}

to install the apk check this 安装apk检查这个

to exit the app use finish(); 退出应用程序使用finish();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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