Retrieving a row resource from another application

So, I want to get a string resource from another application. The application is an Android PackageInstaller (most likely a System application), and the line I want to get has several language versions (source code here> Link ). Thus, the Package Installer's resource directory is as follows:

enter image description here

I want to get String permission_warning_template from the values folder (I understand that Android will automatically detect the current language and bring me that language value, but I could be wrong).

These are two methods that I wrote (a minimal, complete and tested example), and none of them work. Is it possible? How to do it?

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //First Try testUseAndroidString(); //Second Try Resources res = null; try { res = getPackageManager().getResourcesForApplication("com.android.packageinstaller"); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if(null != res) { int sId = res.getIdentifier("com.android.packageinstaller:string/permission_warning_template", null, null); if(0 != sId) { Log.d("TagLet", res.getString(sId)); } } } public void testUseAndroidString() { Context context = this; Resources res; try { res = context.getPackageManager().getResourcesForApplication("com.android.packageinstaller"); int resourceId = res.getIdentifier("com.android.packageinstaller:string/permission_warning_template", null, null); if(0 != resourceId) { CharSequence s = context.getPackageManager().getText("com.android.packageinstaller:string/permission_warning_template", resourceId, null); Log.d("TagLet", "resource=" + s); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } Log.d("TagLet", "FAIL"); } } 

Thanks.

+5
source share
1 answer

So, you have practically solved your problem. Since you have the resources of another application, you can just call get{Something} on this res object and you will get the exact resource.

So, I tried this method:

 public void testUseAndroidString() { Context context = this; Resources res; try { res = context.getPackageManager().getResourcesForApplication("com.android.packageinstaller"); int resourceId = res.getIdentifier("com.android.packageinstaller:string/permission_warning_template", null, null); if(0 != resourceId) { String s = res.getString(resourceId); Log.d("TagLet", "resource=" + s); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } 

And received

Allow% 1 $ s to% 2 $ s?

I think this is exactly what you are looking for.

+3
source

Source: https://habr.com/ru/post/1272920/


All Articles