2010-07-11 225 views
1

我的電池公司合作安裝,我們的手機都是基於Android(SDK 1.5),我們必須登錄我們的手機我們的應用程序,並給它的固件權限,如能力:錯誤具有管理員權限

安裝一個外部。在使用PackageManager應用程序(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) 

這段代碼被執行後(第二種方式),電話繼續正常,但我得到這個異常:

十二月9日至12日:10:16.484:ERROR/PackageManager(6601):無法包文件複製到臨時文件。

+0

我要做的第一件事就是查看PackageManagerService.java:3634處的代碼行。我試着在Google Code上尋找它,但這是HEAD版本,而不是v1.5版本:http://www.google.com/codesearch/p?hl=zh-CN#uX1GffpyOZk/services/java/com/android/ server/PackageManagerService.java&q = PackageManagerService.java&sa = N&cd = 1&ct = rc – MatrixFrog 2010-09-12 23:15:37

+0

您是否曾經解決過這個問題? – Rob 2011-10-10 01:19:27

回答

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,所以我就開始評估的fileNameUri.parse(src.getAbsolutePath())值。

+0

源在http://www.netmite.com/android/mydroid/1.5/frameworks/base/services/java/com/android/server/PackageManagerService.java – Rob 2010-09-16 17:54:15

+0

喜羅布,所以你覺得我應該調整代碼?該文件已經在sdcard中,我應該用其他方式改變它? – rayman 2010-09-19 08:42:28

+0

'Uri.parse(uriString)'的文檔預計'uriString'是一個RFC 2396兼容的編碼URI。我期望的問題是你傳遞了一個本地路徑'src.GetAbsolutePath()',它不符合這個要求。但是,您需要檢查'fileName'變量的值和'getAbsolutePath()'的返回值來驗證這一點。 – Rob 2010-09-21 13:13:10