找不到处理Intent的活动(act = android.intent.action.VIEW)尝试安装APK

时间:2016-10-18 01:24:05

标签: java android android-intent

我正在尝试安装我刚刚下载的APK。但是,当我利用意图安装APK时,我得到错误android.content.ActivityNotFoundException:找不到处理Intent的Activity ... 这是我的来源:

class DownloadTask extends AsyncTask<String, Integer, String> {
    private Context context;
    private String output;
    private Boolean install;
    private String file;
    private PowerManager.WakeLock wakeLock;

    public DownloadTask(Context context, String output, String file, Boolean install) {
        this.context = context;
        this.output = output;
        this.install = install;
        this.file = file;
    }

    @Override
    protected String doInBackground(String... surl) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection c = null;
        try {
            URL url = new URL(surl[0]);
            c = (HttpURLConnection) url.openConnection();
            c.connect();
            if (c.getResponseCode() != HttpURLConnection.HTTP_OK)
                return "Server returned HTTP " + c.getResponseCode() + " " + c.getResponseMessage();
            int filelength = c.getContentLength();
            input = c.getInputStream();
            output = new FileOutputStream(this.output + this.file);
            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                if (filelength > 0)
                    publishProgress((int) (total * 100 / filelength));
                output.write(data, 0, count);
            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (output != null) output.close();
                if (input != null) input.close();
            } catch (IOException e) {
            }
            if (c != null) c.disconnect();
        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
        wakeLock.acquire();
        pDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        pDialog.setIndeterminate(false);
        pDialog.setMax(100);
        pDialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        wakeLock.release();
        pDialog.dismiss();
        if (result != null)
            Toast.makeText(context, "Download error: " + result, Toast.LENGTH_LONG).show();
        else if (this.install) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse("file://" + this.output + this.file), "application/vnd.android.package-archive");
            this.context.startActivity(intent);
        }
    }

}

这是错误的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.lastboxusa.lastboxinstaller, PID: 3076
              android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///data/data/com.lastboxusa.lastboxinstaller/kodi.apk typ=application/vnd.android.package-acrhive }
                  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
                  at android.app.Activity.startActivityForResult(Activity.java:3424)
                  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                  at android.app.Activity.startActivityForResult(Activity.java:3385)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
                  at android.app.Activity.startActivity(Activity.java:3627)
                  at android.app.Activity.startActivity(Activity.java:3595)
                  at com.lastboxusa.lastboxinstaller.MainActivity$DownloadTask.onPostExecute(MainActivity.java:136)
                  at com.lastboxusa.lastboxinstaller.MainActivity$DownloadTask.onPostExecute(MainActivity.java:59)
                  at android.os.AsyncTask.finish(AsyncTask.java:632)
                  at android.os.AsyncTask.access$600(AsyncTask.java:177)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5017)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

问题与apk的名称不是原始名称有关。将其重命名为原始名称。

答案 1 :(得分:0)

使用此代码:

    if (Build.VERSION.SDK_INT >= 29) {

                        try {
//                        DisplayUtils.getInstance().displayLog(TAG, "download completed trying to open application::::::::" + file.getAbsolutePath());
                            DisplayUtils.getInstance().displayLog(TAG, "path::::" + Environment.getExternalStorageDirectory() + "/Documents");
                            Intent installApplicationIntent = new Intent(Intent.ACTION_VIEW);
                                File file = new File(Environment.getExternalStorageDirectory() + "/Documents", "yourfile.apk");
                                if (file.exists()) {
                                    DisplayUtils.getInstance().displayLog(TAG, "is readable:::::::::" + file.canRead());
                                    file.setReadable(true);
                                    installApplicationIntent.setDataAndType(FileProvider.getUriForFile(context,
                                            BuildConfig.APPLICATION_ID + ".provider",
                                            file), "application/vnd.android.package-archive");
                                } else {
                                    DisplayUtils.getInstance().displayLog(TAG, "file not found after downloading");
                                }
                            installApplicationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                            installApplicationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            installApplicationIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            context.startActivity(installApplicationIntent);
                            ExitActivity.exitApplicationAnRemoveFromRecent(context);


} catch (Exception cv)
                        {

                        }

                    }