2013-08-05 70 views
0

我需要根據服務器的最新版本卸載並重新安裝apk文件。如果我沒有在Google Play中更新我的應用程序,我該如何卸載並重新安裝應用程序?我卸載代碼:從java代碼中卸載apk文件

public void UnInstallApplication(String packageName)// Passing com.example.homelauncher as package name. 
    { 

     Uri packageURI = Uri.parse(packageName.toString()); 
     Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); 
     uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(uninstallIntent); 
    } 

我得到以下錯誤:

08-05 20:07:29.670: E/AndroidRuntime(845): FATAL EXCEPTION: main 
08-05 20:07:29.670: E/AndroidRuntime(845): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.DELETE dat=com.example.homelauncher flg=0x10000000 } 
08-05 20:07:29.670: E/AndroidRuntime(845): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409) 
08-05 20:07:29.670: E/AndroidRuntime(845): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379) 
08-05 20:07:29.670: E/AndroidRuntime(845): at android.app.Activity.startActivityForResult(Activity.java:2827) 
08-05 20:07:29.670: E/AndroidRuntime(845): at android.app.Activity.startActivity(Activity.java:2933) 
08-05 20:07:29.670: E/AndroidRuntime(845): at com.example.homelauncher.MainActivity.UnInstallApplication(MainActivity.java:246) 
08-05 20:07:29.670: E/AndroidRuntime(845): at com.example.homelauncher.MainActivity$2$1.onClick(MainActivity.java:152) 
08-05 20:07:29.670: E/AndroidRuntime(845): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159) 
08-05 20:07:29.670: E/AndroidRuntime(845): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-05 20:07:29.670: E/AndroidRuntime(845): at android.os.Looper.loop(Looper.java:123) 
08-05 20:07:29.670: E/AndroidRuntime(845): at android.app.ActivityThread.main(ActivityThread.java:3683) 
08-05 20:07:29.670: E/AndroidRuntime(845): at java.lang.reflect.Method.invokeNative(Native Method) 
08-05 20:07:29.670: E/AndroidRuntime(845): at java.lang.reflect.Method.invoke(Method.java:507) 
08-05 20:07:29.670: E/AndroidRuntime(845): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-05 20:07:29.670: E/AndroidRuntime(845): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-05 20:07:29.670: E/AndroidRuntime(845): at dalvik.system.NativeStart.main(Native Method) 

誰能告訴我如何做到這一點不使用谷歌玩?

+1

在情況下,它是:「從谷歌Play下載的應用不得修改,更換或使用比谷歌Play的更新機制以外的任何方式更新自己的APK二進制代碼。」 (=您的應用可能會被刪除)[via](http://techcrunch.com/2013/04/26/google-crashes-facebook-homes-easy-updating-party-now-requires-all-play-apps-即將更新通過播放商店/) – zapl

回答

1

你正在形成你的URI不正確。有關相關問題,請參閱this SO post(幾乎是重複的)。我已經複製了下面的相關詳細信息:基本上,您需要使用以下代碼來構建URI以使intent方法起作用。

Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package", 
getPackageManager().getPackageArchiveInfo(apkUri.getPath(), 0).packageName,null)); 
startActivity(intent); 
+0

我試過這個代碼,但這個問對話框你想卸載這個apk。我不需要詢問對話框需要卸載並安裝最新版本。 –

+1

@VinothKumar查看上面的評論到您的帖子。這是不允許的。 –

0

如果您的設備是rootet,則可以使用在Android Linux shell中執行的adb命令來卸載apk。

String command = "pm uninstall com.my.first.program ";