问题描述
我想获取设备中存在的所有程序包的活动信息(例如configchanges,resizemode,如果支持画中画).
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.
我可以使用带有 GET_ACTIVITIES
标志的 PackageManager
来获取活动信息.这样,我可以使用 ActivityInfo.configChanges
获得 configChanges
值.
I am able to fetch the activities info using PackageManager
with GET_ACTIVITIES
flag. With that I can get configChanges
value using ActivityInfo.configChanges
.
但是,如果在 android:configChanges
中设置了多个配置值,则该值将返回一个随机整数.
However the value returns a random int if there are multiple config values set in android:configChanges
.
例如:
如果设置了以下值
使用以下代码获取configchanges值
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;
}
}
我得到 activity.configChanges
值为23047
I get activity.configChanges
value as 23047
23047代表什么,我该如何对其进行解码,以便获得在 AndroidManifest.xml
What does 23047 denotes, how do I decode it so that I can get the config values that are set in AndroidManifest.xml
除此之外,我们还有任何方法可以获取 activity.resizeMode
.我了解这是 @hide
api.我可以在Android Studio中以调试模式看到该值.
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.
推荐答案
configChanges
是位掩码.
要检查是否设置了给定的位,您只需要使用适当的按位运算符.
To check if a given bit is set, you simply need to use an appropriate bitwise operator.
例如,要检查是否设置了 uiMode
,您可以执行以下操作:
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;
}
您会这样称呼它:
int configChanges = activityInfo.configChanges;
boolean uiModeSet = isConfigSet(configChanges, ActivityInfo.CONFIG_UI_MODE);
boolean colorModeSet = isConfigSet(configChanges, ActivityInfo.CONFIG_COLOR_MODE);
// ...
可靠,不.尽管Google发布了博客帖子最近声明了以下内容:
Reliably, no. You might be able to access it through the reflection API, although Google released a blog post recently stating the following:
(强烈建议不要通过反射访问隐藏字段)
(accessing hidden fields via reflection is strongly discouraged anyway)
这篇关于如何从ActivityInfo类获取android:configChanges值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!