How to get android:configChanges values from ActivityInfo class

天涯浪子 提交于 2021-02-10 06:19:21

问题


I would like to fetch activities info(Such as configchanges, resizemode, if Picture in Picture is supported) of all the packages present in the device.

I am able to fetch the activities info using PackageManager with GET_ACTIVITIES flag. With that I can get configChanges value using ActivityInfo.configChanges.

However the value returns a random int if there are multiple config values set in android:configChanges.

For ex:

if below values are set

android:configChanges="uiMode|smallestScreenSize|locale|colorMode|density"

Getting configchanges value using below code

PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);

ActivityInfo activityInfo[] = packageInfo.activities;

if(activityInfo!=null) {
    for(ActivityInfo activity : activityInfo) {
      int configChange = activity.configChanges;
    }
}

I get activity.configChanges value as 23047

What does 23047 denotes, how do I decode it so that I can get the config values that are set in AndroidManifest.xml

In Addition to that is there any way we can get activity.resizeMode . I understand that it is @hide api. I can see the value in debugging mode though in Android Studio.

Any leads/help to above will be really useful.


回答1:


configChanges is a bit mask.

To check if a given bit is set, you simply need to use an appropriate bitwise operator.

For example, to check if uiMode is set you could do something like this:

int configChanges = activityInfo.configChanges;

if ((configChanges & ActivityInfo.CONFIG_UI_MODE) == ActivityInfo.CONFIG_UI_MODE) {
    // uiMode is set
} else {
    // uiMode is not set
}

Defining a method might make it easier:

public boolean isConfigSet(int configMask, int configToCheck) {
    return (configMask & configToCheck) == configToCheck;
}

And you would call it like this:

int configChanges = activityInfo.configChanges;

boolean uiModeSet = isConfigSet(configChanges, ActivityInfo.CONFIG_UI_MODE);
boolean colorModeSet = isConfigSet(configChanges, ActivityInfo.CONFIG_COLOR_MODE);
// ...

In Addition to that is there any way we can get activity.resizeMode . I understand that it is @hide api.

Reliably, no. You might be able to access it through the reflection API, although Google released a blog post recently stating the following:

Starting in the next release of Android, some non-SDK methods and fields will be restricted so that you cannot access them -- either directly, via reflection, or JNI.

(accessing hidden fields via reflection is strongly discouraged anyway)



来源:https://stackoverflow.com/questions/50127593/how-to-get-androidconfigchanges-values-from-activityinfo-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!