android - 如何检查 Activity 是否启用?

标签 android android-activity android-manifest android-package-managers

背景

我正在尝试检查是否在运行时启用/禁用了 Activity (或任何其他应用组件类型)。

问题

可以使用下一个代码:

    final ComponentName componentName = new ComponentName(context, activityClass);
    final PackageManager pm = context.getPackageManager();
    final int result = pm.getComponentEnabledSetting(componentName);

但是返回的结果,写在the documentation是:

Returns the current enabled state for the component. May be one of COMPONENT_ENABLED_STATE_ENABLED, COMPONENT_ENABLED_STATE_DISABLED, or COMPONENT_ENABLED_STATE_DEFAULT. The last one means the component's enabled state is based on the original information in the manifest as found in ComponentInfo.

所以它不仅仅是启用/禁用,而且是“默认”。

问题

如果返回“COMPONENT_ENABLED_STATE_DEFAULT”,我如何知道它是默认启用还是禁用(在运行时)?

这个问题的原因是无论人们在 list 中放入什么代码都应该可以工作(对于“启用”属性)。

是否可以使用 Intent 解析?

最佳答案

If COMPONENT_ENABLED_STATE_DEFAULT is returned, how do I know if it's default as enabled or disabled?

您将需要使用 PackageManager 加载所有组件并检查匹配 ComponentInfo 的启用状态。以下代码应该可以工作:

public static boolean isComponentEnabled(PackageManager pm, String pkgName, String clsName) {
  ComponentName componentName = new ComponentName(pkgName, clsName);
  int componentEnabledSetting = pm.getComponentEnabledSetting(componentName);

  switch (componentEnabledSetting) {
    case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
      return false;
    case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
      return true;
    case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
    default:
      // We need to get the application info to get the component's default state
      try {
        PackageInfo packageInfo = pm.getPackageInfo(pkgName, PackageManager.GET_ACTIVITIES
            | PackageManager.GET_RECEIVERS
            | PackageManager.GET_SERVICES
            | PackageManager.GET_PROVIDERS
            | PackageManager.GET_DISABLED_COMPONENTS);

        List<ComponentInfo> components = new ArrayList<>();
        if (packageInfo.activities != null) Collections.addAll(components, packageInfo.activities);
        if (packageInfo.services != null) Collections.addAll(components, packageInfo.services);
        if (packageInfo.providers != null) Collections.addAll(components, packageInfo.providers);

        for (ComponentInfo componentInfo : components) {
          if (componentInfo.name.equals(clsName)) {
            return componentInfo.isEnabled();
          }
        }

        // the component is not declared in the AndroidManifest
        return false;
      } catch (PackageManager.NameNotFoundException e) {
        // the package isn't installed on the device
        return false;
      }
  }
}

在我的设备上测试上述代码:

System.out.println(isComponentEnabled(getPackageManager(),
    "com.android.systemui",
    "com.android.systemui.DessertCaseDream"));

System.out.println(isComponentEnabled(getPackageManager(),
    "com.android.settings",
    "com.android.settings.DevelopmentSettings"));

false

true

关于android - 如何检查 Activity 是否启用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26956375/

相关文章:

java - 一项 Activity 中的多个 fragment

android - 将 Activity 1 中的 View 转换/动画化为 Activity 2 中的 View

Android 选项卡 - 避免在选项卡点击时重新创建 Activity

java - 何时使用权限 WRITE_CONTACT 和 READ_CONTACT?

android - 如何在 Android 11 中创建从图库中选择图像的 Intent ?

android - 使用安卓手机测北

android - fragment 必须是公共(public)静态类才能从实例状态正确地重新创建

android - 在平板电脑上膨胀 GridView 时出错

Android 从 activity/asynctask 启动服务

android - 合并 list 中的 NetworkSecurityConfig xml 文件