簡體   English   中英

未找到處理 Intent 的活動 (act=android.intent.action.VIEW) 正在嘗試安裝 APK

[英]No Activity Found to handle Intent (act=android.intent.action.VIEW) Trying to Install APK

我正在嘗試安裝我剛剛下載的 APK。 但是,當我利用 Intent 安裝 APK 時,出現錯誤 android.content.ActivityNotFoundException:找不到處理 Intent 的活動...這是我的來源:

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)

問題與 apk 的名稱不是原始名稱有關。 將其重命名為原來的名稱有效。

使用此代碼:

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

//這對我有用。

public class UpdateApk {

    UpdateApk() {}

    public void Update(File downloaded_apk, Context context ){
        if (Build.VERSION.SDK_INT >= 28) {
            try {
                Intent installApplicationIntent = new Intent(Intent.ACTION_VIEW);
                if (downloaded_apk.exists()) {
                    downloaded_apk.setReadable(true);
                    installApplicationIntent.setDataAndType(FileProvider.getUriForFile(context,
                            BuildConfig.APPLICATION_ID + ".provider",
                            downloaded_apk), "application/vnd.android.package-archive");
                } else {
                    Log.wtf("qwe", "UpdateApk No File");
                }
                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);
            } catch (Exception cv) {
                Log.wtf("qwe", "UpdateApk "+cv.getMessage());
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM