以编程方式在Marshmallow上安装APK

时间:2016-05-16 05:09:09

标签: android android-intent apk android-6.0-marshmallow

在我的一个用例中,应用程序在开头联系服务器并下载同一应用程序的更新。

下载我正在使用Android的DownloadManager类。 然后使用下面的代码尝试在onReceive()中创建应用安装程序意图,即在成功下载APK之后。

@Override
public void onReceive(Context context, Intent intent) {

    //check if the broadcast message is for our Enqueued download
    long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    if (downloadReference == referenceId) {
        try {
            Log.v("", "Downloading of the new app version complete");

            String filepath = Environment.getExternalStorageDirectory() + "/" +
                    Environment.DIRECTORY_DOWNLOADS + "/" + mLatestVersionFileName;

            //start the installation of the latest version
            Uri uri = downloadManager.getUriForDownloadedFile(downloadReference);

            //downloadManager.getUriForDownloadedFile(downloadReference)
            Uri fileLoc = Uri.fromFile(new File(filepath));

            Intent promptInstall = new Intent(Intent.ACTION_VIEW);
            promptInstall.setDataAndType(uri, "application/vnd.android.package-archive");
            promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(promptInstall);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

该代码适用于Pre-Marshmallow设备,用户可以通过屏幕安装APK。

这不适用于Marshmallow设备。

我在Marshmallow上遇到以下异常:

  

android.content.ActivityNotFoundException:找不到处理Intent的Activity {act = android.intent.action.VIEW dat = content:// downloads / my_downloads / 287 typ = application / vnd.android.package-archive flg = 0x10000000 }

我试过以下事情:

  1. 使用Marshmallow (com.google.android.packageinstaller/com.android.packageinstaller.PackageInstallerActivity)组件名称的确切PackageInstaller作为Intent的一部分。
  2. 传递fileLoc而不是uri。
  3. promptInstall.setDataAndType(uri, "application/vnd.android.package-archive");
    

    这导致'解析错误'。

    理想情况下,Android OS会解决意图。我想这不是一个大问题,我可能在创建Intent时遗漏了一些东西。

2 个答案:

答案 0 :(得分:3)

你可以试试这个:

@Override
public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

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

            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(yourDownloadID);
      DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
            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);

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

答案 1 :(得分:3)

试试这个

 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);
                    Query query = new 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)) {

                            ImageView view = (ImageView) findViewById(R.id.imageView1);
                            String uriString = c
                                    .getString(c
                                            .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

                             //start the installation of the latest version
                            Uri uri = Uri.parse(uriString); 
                            Intent promptInstall = new Intent(Intent.ACTION_VIEW);
                            promptInstall.setDataAndType(uri, "application/vnd.android.package-archive");
                            promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(promptInstall);
                        }
                    }
                }
            }