我正在尝试从外部 apk 文件开始活动。apk 文件是一个简单的文本显示,我从 URL 将它下载到 /mnt/sdcard/ 中的本地安装目录中。到那时一切都很好。现在,我想从该文件开始一个活动,但我遇到了错误。两种类型,取决于 Intent 构造。下面是关联 LogCat 的两个实现,“targetFilePath”是相同的。案例一:
try {
Intent install = new Intent(Intent.ACTION_VIEW)
.setData(Uri.fromFile(new File(targetFilePath)))
.setType("application/vnd.android.package-archive");
startActivity(install);
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(DownloadFile.this, "apk file launch error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
日志猫:
08-26 17:03:02.153: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.VIEW typ=application/vnd.android.package-archive }
08-26 17:03:02.153: W/System.err(390): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=application/vnd.android.package-archive }
08-26 17:03:02.162: W/System.err(390): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
08-26 17:03:02.162: W/System.err(390): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
08-26 17:03:02.162: W/System.err(390): at android.app.Activity.startActivityForResult(Activity.java:2817)
08-26 17:03:02.162: W/System.err(390): at android.app.Activity.startActivity(Activity.java:2923)
08-26 17:03:02.162: W/System.err(390): at com.agorasoft.sandbox.DownloadFile$1.onClick(DownloadFile.java:137)
08-26 17:03:02.162: W/System.err(390): at android.view.View.performClick(View.java:2408)
08-26 17:03:02.172: W/System.err(390): at android.view.View$PerformClick.run(View.java:8816)
08-26 17:03:02.172: W/System.err(390): at android.os.Handler.handleCallback(Handler.java:587)
08-26 17:03:02.172: W/System.err(390): at android.os.Handler.dispatchMessage(Handler.java:92)
08-26 17:03:02.172: W/System.err(390): at android.os.Looper.loop(Looper.java:123)
08-26 17:03:02.182: W/System.err(390): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-26 17:03:02.182: W/System.err(390): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 17:03:02.182: W/System.err(390): at java.lang.reflect.Method.invoke(Method.java:521)
08-26 17:03:02.182: W/System.err(390): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-26 17:03:02.182: W/System.err(390): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-26 17:03:02.182: W/System.err(390): at dalvik.system.NativeStart.main(Native Method)
08-26 17:03:21.761: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
案例二:
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri apkUri = Uri.fromFile(new File(targetFilePath));
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
startActivity(intent);
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(DownloadFile.this, "apk file launch error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
解析错误弹窗:解析包有问题
日志猫:
08-26 17:09:48.862: W/PackageParser(329): Unable to read AndroidManifest.xml of /mnt/sdcard/Downloads/ApkTest.apk
08-26 17:09:48.862: W/PackageParser(329): java.io.FileNotFoundException: AndroidManifest.xml
08-26 17:09:48.862: W/PackageParser(329): at android.content.res.AssetManager.openXmlAssetNative(Native Method)
08-26 17:09:48.862: W/PackageParser(329): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:485)
08-26 17:09:48.862: W/PackageParser(329): at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:453)
08-26 17:09:48.862: W/PackageParser(329): at android.content.pm.PackageParser.parsePackage(PackageParser.java:396)
08-26 17:09:48.862: W/PackageParser(329): at com.android.packageinstaller.PackageUtil.getPackageInfo(PackageUtil.java:79)
08-26 17:09:48.862: W/PackageParser(329): at com.android.packageinstaller.PackageInstallerActivity.onCreate(PackageInstallerActivity.java:242)
08-26 17:09:48.862: W/PackageParser(329): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-26 17:09:48.862: W/PackageParser(329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-26 17:09:48.862: W/PackageParser(329): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-26 17:09:48.862: W/PackageParser(329): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-26 17:09:48.862: W/PackageParser(329): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-26 17:09:48.862: W/PackageParser(329): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 17:09:48.862: W/PackageParser(329): at android.os.Looper.loop(Looper.java:123)
08-26 17:09:48.862: W/PackageParser(329): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-26 17:09:48.862: W/PackageParser(329): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 17:09:48.862: W/PackageParser(329): at java.lang.reflect.Method.invoke(Method.java:521)
08-26 17:09:48.862: W/PackageParser(329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-26 17:09:48.862: W/PackageParser(329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-26 17:09:48.862: W/PackageParser(329): at dalvik.system.NativeStart.main(Native Method)
08-26 17:09:48.862: W/PackageInstaller(329): Parse error when parsing manifest. Discontinuing installation
08-26 17:09:49.902: I/ActivityManager(59): Displayed activity com.android.packageinstaller/.PackageInstallerActivity: 1168 ms (total 1168 ms)
对于冗长的论点感到抱歉,但似乎很奇怪,首先,它不起作用,然后我对意图/安装定义的两种语法有不同的行为。调用应用程序和 apk 文件具有相同的 SDK 目标/最小版本。在调用应用程序清单中没有为此代码块设置特殊权限(我需要任何权限吗?)。谢谢