2013-08-05 52 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 ";