Android Question Possible to get value from strings.xml for other app?

Sandman

Expert
Licensed User
Longtime User
Many apps have their strings (in many languages) in strings.xml. Can we get some of their string values?

If we run this...
B4X:
Dim ctxt As JavaObject
ctxt.InitializeContext
ctxt.InitializeArray
Dim PackageManager As JavaObject = ctxt.RunMethod("getPackageManager", Null)
Log(PackageManager.RunMethod("getBackgroundPermissionOptionLabel", Null))
...we'll get something like "Allow all the time", depending on the language of the device and API level (should be at least 30).

This is the code for getBackgroundPermissionOptionLabel, used above:
B4X:
public CharSequence getBackgroundPermissionOptionLabel() {
    try {

        String permissionController = getPermissionControllerPackageName();
        Context context =
                mContext.createPackageContext(permissionController, 0);

        int textId = context.getResources().getIdentifier(APP_PERMISSION_BUTTON_ALLOW_ALWAYS,
                "string", PERMISSION_CONTROLLER_RESOURCE_PACKAGE);
        if (textId != 0) {
            return context.getText(textId);
        }
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Permission controller not found.", e);
    }
    return "";
}

And here are the strings.xml that getBackgroundPermissionOptionLabel get values from:

And here are the list of languages that the file exist in:


My question: Using the example code above, is it possible to use JavaObject (or something like that) get some other string, in the language of the device? (And I'm only focused on strings for PermissionController.)
 
Solution
Since the "getBackgroundPermissionOptionLabel" is only available since API30, I have taken another approach.
This has worked for me (tested with Android 10).
There are changes in Android 11 regarding the Packagemanager, that not all apps will be by default visible to the user. However, I don't think this affects system apps

Pls confirm if this works for you
B4X:
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim PackageManager As JavaObject = ctxt.RunMethod("getPackageManager", Null)
      
    Dim res As JavaObject = PackageManager.RunMethod("getResourcesForApplication", Array("com.google.android.permissioncontroller"))
          
    Dim resId As Int = res.RunMethod("getIdentifier"...

Erel

B4X founder
Staff member
Licensed User
Longtime User
This line doesn't do anything useful: ctxt.InitializeArray
I've tried with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim id As Int = GetResourceId("string", "app_permission_button_allow_always", "com.android.permissioncontroller")
    Log(id)
End Sub

Sub GetResourceId(ResourceType As String, Name As String, PackageName As String) As Int
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Return ctxt.RunMethodJO("getResources", Null).RunMethod("getIdentifier", Array(Name, ResourceType, PackageName))
End Sub

It didn't work. You will need to do some research.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
You will need to do some research.
I tried a bunch of variations, which is about as useful as having a monkey pound the keyboard, considering I don't really understand how to use JavaObjects.

I did, however, find a very small snippet at GitHub that seemed to be relevant. I noticed that it differs slightly from code in #2:
B4X:
' From GitHub code
context.resources.getString(R.string.geofence_unknown_error) 

' Code from #2
ctxt.RunMethodJO("getResources", Null).RunMethod("getIdentifier", Array(Name, ResourceType, PackageName))

Does this help at all?
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Since the "getBackgroundPermissionOptionLabel" is only available since API30, I have taken another approach.
This has worked for me (tested with Android 10).
There are changes in Android 11 regarding the Packagemanager, that not all apps will be by default visible to the user. However, I don't think this affects system apps

Pls confirm if this works for you
B4X:
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim PackageManager As JavaObject = ctxt.RunMethod("getPackageManager", Null)
      
    Dim res As JavaObject = PackageManager.RunMethod("getResourcesForApplication", Array("com.google.android.permissioncontroller"))
          
    Dim resId As Int = res.RunMethod("getIdentifier", Array("app_permission_button_allow_always", "string", "com.android.permissioncontroller"))

    ' Resource string in current lang, whatever it is
    Log($"Current ${"app_permission_button_allow_always"} resource string is: ${res.RunMethod("getString", Array(resId))}"$)  
  
    ' Change locale for resources
    Dim LocaleList As JavaObject
    LocaleList.InitializeStatic("android.os.LocaleList")
    LocaleList = LocaleList.RunMethod("forLanguageTags", Array("it"))   '<-- HERE!

    Dim currentConfig As JavaObject = res.RunMethod("getConfiguration", Null)  
    currentConfig.RunMethod("setLocales", Array(LocaleList))
    ' Deprecated, but still works
    res.RunMethod("updateConfiguration", Array( currentConfig, res.RunMethod("getDisplayMetrics", Null)))
  
    ' Now it will be in italian
    Log($"New ${"app_permission_button_allow_always"} resource string is: ${res.RunMethod("getString", Array(resId))}"$)
 
Last edited:
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…