So I want to retrieve a string resource from another app. The app is the Android PackageInstaller
(most probably a System
app) and the String I want to retrieve has multiple language versions (Source Code here > Link). So Package Installer's
resource directory looks like this:
I want to retrieve the String permission_warning_template
from the values
folder (I realize that Android will automatically determine the current language and will fetch me that language's value, but I may be wrong).
These are the 2 methods I wrote (Minimal, Complete, and Verifiable example) and neither works. Is it even possible? How to get it done?
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.
So you actually almost solved your problem. As you have the resources of another application retreived, you just have to 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 got
Allow %1$s to %2$s?
Which I guess is exactly what you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With