enable/disable packages programmatically

时间:2016-07-11 20:37:39

标签: android android-package-managers

How can I enable/disable a whole package programmatically from a Android System application?

In this post, a component is enabled/disabled programmatically, but not a whole package.

I want to achieve the same result that is achieved by the root command pm disable/enable package_name (from the adb shell -> su).

1 个答案:

答案 0 :(得分:0)

两种方法可以做到这一点:

  1. 使用pm disable命令:

    Runtime runtime = Runtime.getRuntime();
    runtime.exec("pm disable "+packageName);
    
  2. 使用PackageManager API:

    public abstract void setApplicationEnabledSetting (String packageName, 
                                                       int newState, int flags)
    

在newState参数中,您可以传递“ COMPONENT_ENABLED_STATE_DISABLED”以禁用该应用程序,并传递“ COMPONENT_ENABLED_STATE_ENABLED”以启用该应用程序。

参考:https://developer.android.com/reference/android/content/pm/PackageManager#setApplicationEnabledSetting(java.lang.String,%20int,%20int)