简体   繁体   English

在Android中静默卸载应用程序?

[英]uninstall app silently in android?

I want to delete application silently from device. 我想从设备静默删除应用程序。 i am trying this code but gives exception "Neither user 10051 nor current process has android.permission.DELETE_PACKAGES." 我正在尝试此代码,但给出异常“用户10051或当前进程都没有android.permission.DELETE_PACKAGES。”

 class PackageInstallObserver extends IPackageInstallObserver.Stub {
    public void packageInstalled(String packageName, int returnCode) throws RemoteException {
        if (onInstalledPackaged != null) {
            onInstalledPackaged.packageInstalled(packageName, returnCode);
        }
    }
}

class PackageDeleteObserver extends IPackageDeleteObserver.Stub { 

    public void packageDeleted(String packageName, int returnCode) throws RemoteException {
        /*if (onInstalledPackaged != null) {
            onInstalledPackaged.packageInstalled(packageName, returnCode);
        }*/
    }
}

public ApplicationManager(Context context) throws SecurityException, NoSuchMethodException {

    observer = new PackageInstallObserver();
    observerdelete = new PackageDeleteObserver(); 
    pm = context.getPackageManager();



    Class<?>[] types = new Class[] {Uri.class, IPackageInstallObserver.class, int.class, String.class};
    Class<?>[] uninstalltypes = new Class[] {String.class, IPackageDeleteObserver.class, int.class};

    method = pm.getClass().getMethod("installPackage", types);
      uninstallmethod = pm.getClass().getMethod("deletePackage", uninstalltypes);
}

public void setOnInstalledPackaged(OnInstalledPackaged onInstalledPackaged) {
    this.onInstalledPackaged = onInstalledPackaged; 
}

public void uninstallPackage(String packagename) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    uninstallmethod.invoke(pm, new Object[] {packagename, observerdelete, 0});


}

It is impossible to silently delete an Android app programmatically . 以编程方式静默删除Android应用程序是不可能的

The best you can do is ask the user to delete the app in a Dialog. 最好的办法是让用户在对话框中删除该应用程序。 When he presses "OK" then redirect to the Uninstaller application pointed to the application. 当他按“确定”时,然后重定向到指向该应用程序的Uninstaller应用程序。 It is still the user's choice if he wants to complete the delete operation. 用户是否要完成删除操作仍然是用户的选择。

There is a way to insist that the user finishes the delete operation, but it is rather bullish. 有一种方法可以坚持要求用户完成删除操作,但它相当乐观。 After he closes the Uninstaller application he will return to your Activity in onActivityResult() . 关闭卸载程序后,他将在onActivityResult()返回到您的活动。 You can check to see if the app is deleted. 您可以检查该应用是否已删除。 If it is not yet deleted you can return to the same Dialog and ask him again to delete the application. 如果尚未删除,则可以返回到同一对话框,然后再次要求他删除该应用程序。 You can continue in this endless loop if you choose until the user finally deletes the app or stops using your main application (ie You are telling the user he can't use your app until he deletes the other app). 如果您选择直到用户最终删除该应用程序或停止使用您的主应用程序(例如,您告诉用户他要删除其他应用程序后才能使用您的应用程序),则可以继续循环进行。

How to check if an app is installed : 如何检查是否已安装应用程序

final String packageName = "com.company.other-app-name";
android.content.pm.PackageManager pm = getPackageManager();
PackageInfo info = pm.getPackageInfo(packageName, 0);

If info==null or a NameNotFoundException is thrown, then the app is not installed. 如果info==null或抛出NameNotFoundException ,则表示未安装该应用程序。

How to launch the uninstaller pointed to an app : 如何启动卸载程序指向一个应用程序

Uri uri = Uri.parse("package:com.company.other-app-name");
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivityForResult(intent, REQUEST_CODE_UNINSTALL_FREE_APP);

SDK applications cannot have DELETE_PACKAGES permissions, unless they are part of the firmware. SDK应用程序不能具有DELETE_PACKAGES权限,除非它们是固件的一部分。 Which means that an app that is not compiled with firmware does not have/acquire right to delete apps silently.. 这意味着未使用固件编译的应用程序无权获得无提示删除应用程序的权限。

check out this post for more info.. 查看此帖子以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM