[英]No Activity Found to handle Intent (act=android.intent.action.VIEW) Trying to Install APK
I'm trying to install an APK that I just downloaded.我正在尝试安装我刚刚下载的 APK。 However, when I utilize the intent to install the APK I get error android.content.ActivityNotFoundException: No Activity found to handle Intent... Here's my source:但是,当我利用 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);
}
}
}
And here is the stacktrace of the error:这是错误的堆栈跟踪:
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)
The problem had to do with the fact that the name of the apk was not the original.问题与 apk 的名称不是原始名称有关。 Renaming it to its original name worked.将其重命名为原来的名称有效。
use this code:使用此代码:
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) {
}
}
//This worked for me. //这对我有用。
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.