The code of Jared Rummler is good and it work but it takes the status in the manifest.
To have the current status of a component inside my app I needed to create the "ComponentName" through the "Package Context" and the "Component Class" and not the "Package Name" and "Class Name".
I had a BroadcastReceiver setted to "enabled = false" in the manifest, after that I enabled it inside my app using the package manager, but the Jared Rummler's codes always return me "STATE_ENABLED_DEFAULT" and "enabled and isEnabled()" always returned false.
Using the "Package Context" and the "Component Class" i get directly the "ENABLED_STATE_DISABLED" and "ENABLED_STATE_ENABLED" without using the code in the default part, also the "enabled and isEnabled()" returned me anyway FALSE if I've started the receiver using the package manager.
Hope this is useful, see u
public static void enableDisableComponent(Context pckg, Class componentClass, boolean enable){
ComponentName component = new ComponentName(pckg, componentClass);
if(enable == !checkIfComponentIsEnabled(pckg, componentClass)) {
pckg.getPackageManager().setComponentEnabledSetting(
component,
enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
);
}
}
public static boolean checkIfComponentIsEnabled(Context pckg, Class componentClass){
boolean ret = false;
ComponentName componentName = new ComponentName(pckg, componentClass);
int componentEnabled = pckg.getPackageManager().getComponentEnabledSetting(componentName);
switch(componentEnabled){
case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
break;
case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
ret = true;
break;
case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
default:
ret = checkEnabledComponentsInfo(pckg, componentClass);
break;
}
return ret;
}
private static boolean checkEnabledComponentsInfo(Context pckg, Class componentClass){
boolean ret = false;
try{
PackageInfo packageInfo = pckg.getPackageManager().getPackageInfo(
pckg.getPackageName(),
PackageManager.GET_ACTIVITIES | PackageManager.GET_RECEIVERS |
PackageManager.GET_SERVICES | PackageManager.GET_PROVIDERS |
PackageManager.GET_DISABLED_COMPONENTS
);
List<ComponentInfo> componentsInfo = new ArrayList<>();
if(packageInfo.activities != null && packageInfo.activities.length > 0){
Collections.addAll(componentsInfo, packageInfo.activities);
}
if(packageInfo.services != null && packageInfo.services.length > 0){
Collections.addAll(componentsInfo, packageInfo.services);
}
if(packageInfo.providers != null && packageInfo.providers.length > 0){
Collections.addAll(componentsInfo, packageInfo.providers);
}
if(packageInfo.receivers != null && packageInfo.receivers.length > 0){
Collections.addAll(componentsInfo, packageInfo.receivers);
}
if(componentsInfo.size() > 0){
for(ComponentInfo info : componentsInfo){
if(info.name.equals(componentClass.getName())){
ret = info.isEnabled();
break;
}
}
}
} catch(PackageManager.NameNotFoundException nnfE){
onException(nnfE);
}
return ret;
}
enabled
field which appears to represent the manifest value. However if the encapsulating application is disabled then it modifies the field to false. – DetachmentgetActivityInfo()
and the like, callingComponentInfo.isEnabled()
on them? It says Return whether this component and its enclosing application are enabled. – Yettayettienabled
field but it's not clear what this actually represents as sometimes it's true even when the component enabled setting is one of the two DISABLED states. Which is why I chose to usegetComponentEnabledSetting()
as it always represents what thepm
command does but theCOMPONENT_ENABLED_STATE_DEFAULT
is ambiguous. – Detachment