gpt4 book ai didi

android - 如何以编程方式访问 Android 操作系统的私有(private)字符串?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:44:45 24 4
gpt4 key购买 nike

背景

我想以编程方式获取 Android 操作系统的所有字符串(包括所有字符串),包括那些被认为是私有(private)的字符串。

例如,我想获取 packageManager 应用程序的那些信息,如找到的 here .

问题

使用 android.R.string 只返回一小部分字符串。

我尝试过的

我找到了 this link ,它显示了下一个代码,但我不确定要将什么放入参数中:

private String GetAttributeStringValue(Context context, AttributeSet attrs, String namespace, String name, String defaultValue) 
{
//Get a reference to the Resources
Resources res = context.getResources();
//Obtain a String from the attribute
String stringValue = attrs.getAttributeValue(namespace, name);
//If the String is null
if(stringValue == null)
{
//set the return String to the default value, passed as a parameter
stringValue = defaultValue;
}
//The String isn't null, so check if it starts with '@' and contains '@string/'
else if( stringValue.length() > 1 &&
stringValue.charAt(0) == '@' &&
stringValue.contains("@string/") )
{
//Get the integer identifier to the String resource
final int id = res.getIdentifier(context.getPackageName() + ":" + stringValue.substring(1), null, null);
//Decode the string from the obtained resource ID
stringValue = res.getString(id);
}
//Return the string value
return stringValue;
}

我过去见过一些应用程序可以列出其他应用程序的各种资源,包括系统本身的资源(示例 here)。

后来我找到了如何从您想要的任何应用程序中获取字符串,但它假定您知道标识符的名称,并且不会让您列出它们:

fun getStringFromApp(context: Context, packageName: String, resourceIdStr: String, vararg formatArgs: Any): String? {
try {
val resources = context.packageManager.getResourcesForApplication(packageName)
val stringResId = resources.getIdentifier(resourceIdStr, "string", packageName)
if (stringResId == 0)
return null
return resources.getString(stringResId, *formatArgs)
} catch (e: Exception) {
return null
}
}

例如,如果你想得到字符串“more”(key是“more_item_label”),你可以使用:

val moreString = getStringFromApp(this,"android", "more_item_label")

问题

这样的事情可能吗?如果没有,是否可以使用 root 来完成?

最佳答案

您的链接显示如何从首选项屏幕获取资源并说:

 The goal of this example was to show how to obtain the correct String resource outside the Android XML namespace

链接说明您可以通过替代方式获取资源,而不仅仅是通过R.string。

我使用另一个自定义命名空间和自定义按钮标签创建了一个演示项目,但遗憾的是我无法将字符串资源从一个项目显示到另一个项目。

关于android - 如何以编程方式访问 Android 操作系统的私有(private)字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15635403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com