4

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

ND1010_
  • 3,743
  • 24
  • 41
baloot
  • 49
  • 4

2 Answers2

0
    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));
PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
0

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.

Anbarasu Chinna
  • 975
  • 9
  • 28