简体   繁体   中英

how to download apk and install on android 6

How to download and install an apk file in Android programming. We have problems with Android 6 and above.

dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(file_url));
enqueue = dm.enqueue(request);
    receiver3 = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                long downloadId = intent.getLongExtra(
                        DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(enqueue);
                Cursor c = dm.query(query);
                if (c.moveToFirst()) {
                    int columnIndex = c
                            .getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c
                            .getInt(columnIndex)) {

                        String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
                        String uriString2 = c.getString(c.getColumnIndex(DownloadManager.COLUMN_URI));

                        Intent intent2 = new Intent(Intent.ACTION_VIEW);
                        intent2.setDataAndType(Uri.fromFile(new File(uriString2)), "application/vnd.android.package-archive");
                        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent2);
                    }
                }
            }
        }
    };
    StartActivity.this.registerReceiver(receiver3, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

when download finish and install error:parse error there was a problem parsing the package stack

  dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(file_url)); enqueue = dm.enqueue(request); receiver3 = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { long downloadId = intent.getLongExtra( DownloadManager.EXTRA_DOWNLOAD_ID, 0); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(enqueue); Cursor c = dm.query(query); if (c.moveToFirst()) { int columnIndex = c .getColumnIndex(DownloadManager.COLUMN_STATUS); if (DownloadManager.STATUS_SUCCESSFUL == c .getInt(columnIndex)) { String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE)); String uriString2 = c.getString(c.getColumnIndex(DownloadManager.COLUMN_URI)); File file = new File(dir, "App.apk"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); } } } } }; StartActivity.this.registerReceiver(receiver3, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

May be this link can be useful for you

 https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android

Along with check the following factor that affect installation.

File may be downloaded incompletely. Application might not be suitable for your device config. Security issue - but you can enable from setting option. Corrupted APK file.

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