我为一家手机公司工作,我们的手机基于Android(SDK 1.5),我们有能力在我们的手机上签署我们的应用程序并赋予其固件权限,例如:
使用PackageManager时安装extern应用程序。(SDK 1.5)
这就是我所做的:
File src = context.getFileStreamPath(fileName);
Uri mPackageURI = Uri.parse(src.getAbsolutePath());
PackageManager pm = context.getPackageManager();
int installFlags = 0;
try {
PackageInfo pi = pm.getPackageInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if (pi != null) {
Log.debug(TAG, TAG + ":replacing " + fileName);
installFlags |= PackageManager.REPLACE_EXISTING_PACKAGE;
}
} catch (NameNotFoundException e) { }
try {
PackageInstallObserver observer = new PackageInstallObserver();
pm.installPackage(mPackageURI, observer, installFlags);
} catch (SecurityException e) {
//if the app is not signed by the manufacture it will get here a security exception
}
class PackageInstallObserver extends IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
}
}
现在这是我得到的奇怪的事情:
因为我们是manfucatures,所以在执行时我们不会得到任何异常 pm.installPackage(mPackageURI,observer,installFlags);
因为我们已经使用管理员权限签署了我们的应用程序。
但我们得到的是这个疯狂的结果,手机正在重新启动。
threadid=21: thread exiting with uncaught exception (group=0x4000fe70)
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): Uncaught handler: thread PackageManager exiting due to uncaught exception
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): *** EXCEPTION IN SYSTEM PROCESS. System will crash.
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): java.lang.NullPointerException
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): at
com.android.server.PackageManagerService.installPackageLI(PackageManagerService.java:3634)
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): at com.android.server.PackageManagerService.access$1500(PackageManagerService.java:120)
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): at com.android.server.PackageManagerService$5.run(PackageManagerService.java:3253)
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): at android.os.Handler.handleCallback(Handler.java:587)
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): at android.os.Looper.loop(Looper.java:123)
07-11 16:29:38.493: ERROR/AndroidRuntime(2561): at android.os.HandlerThread.run(HandlerThread.java:60)
第二种方式: 我也尝试过这种方式,我得到了不同的例外:
*与第一种方式非常相似,只是主要区别在于这一行:
String src = String.format("file:///data/data/com.mirs.agentcore/files/%s", fileName);
Uri mPackageURI = Uri.parse(src);
PackageManager pm = context.getPackageManager();
String src = String.format("file:///data/data/com.mirs.agentcore/files/%s", fileName);
Uri mPackageURI = Uri.parse(src);
PackageManager pm = context.getPackageManager();
int installFlags = 0;
.... //the rest of the code is the same as the First way(mention above)
执行此代码后(第二路),手机正常继续,但我得到了这个例外:
09-12 12:10:16.484:ERROR / PackageManager(6601):无法将包文件复制到临时文件。
答案 0 :(得分:0)
我找到了PackageManagerService.java的1.5源代码,这看起来像是导致问题的代码:
try {
fd = mContext.getContentResolver().openFileDescriptor(pPackageURI, "r");
}
catch (FileNotFoundException e) {
Log.e(TAG, "Couldn't open file descriptor from download service.");
res.returnCode = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
// This break statement was line 3634.
break main_flow;
}
documentation for ContentResolver.openFileDescriptor说:
如果不是,则抛出FileNotFoundException 文件存在于URI或模式下 无效。
问题出在你的mPackageURI上,所以我会开始评估fileName
和Uri.parse(src.getAbsolutePath())
的值。