我正在尝试从外部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();
}
Logcat:
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
案例II:
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();
}
解析错误弹出窗口:解析包时出现问题
LogCat:
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)
对于冗长的参数感到抱歉,但似乎很奇怪,首先,它不起作用,然后我对intent / install定义的两个语法有不同的行为。 调用应用程序和apk文件都具有相同的SDK目标/最小版本。调用应用程序清单中没有为此代码块设置特殊权限(我需要吗?)。 感谢
答案 0 :(得分:2)
文档会保留您的答案:Intent.setType
的文档这用于创建仅指定类型而不指定数据的意图,例如用于指示要返回的数据类型。
此方法会自动清除之前设置的所有数据(例如,通过setData(Uri))。
很少使用此方法 - 它允许您覆盖通常根据此处给出的类型从数据中推断出的MIME类型。
简而言之,在构造此intent时不需要指定类型,它将从给定URI的数据本身推断出来。
所有这些都说,看来nandeesh的评论是正确的 - 您尝试安装的软件包看起来很糟糕。同样值得注意的是,如果您打算将此应用程序发布到Play商店,则开发者协议不允许下载或安装其他API的应用程序。