Installiere apk nach dem Download mit dem Download Manager und beende die App

Ich habe eine Android-App erstellt und vom Server wird der Download automatisch gestartet, wenn eine neue Version veröffentlicht wurde, indem der eingebaute "Download-Manager" verwendet wird. Für die automatische Installation nach Abschluss des Downloads habe ich einen Rundfunkempfänger erstellt, um zu informieren, dass der Download abgeschlossen und abgeschlossen ist, und dann beginne ich mit der Installation. Es funktioniert gut, während ich in der App bleibe und sie nicht schließe. Aber mein Problem ist, wenn ich die Anwendung schließe. Nach Abschluss des Downloads möchte ich es automatisch installieren. aber ich werde nicht passieren. Was soll ich für dieses Problem tun?

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

}

Antworten auf die Frage(4)

Ihre Antwort auf die Frage