Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - get manifest meta-data out of activity

Tags:

java

android

I want to do something like this

private static final String url;
private static final String pass;
private static final String user;

static {
    Bundle metadata = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA).metaData;
    url = (String) metadata.get("JMSQueueURL");
    user = (String) metadata.get("JMSQueueUsername");
    pass = (String) metadata.get("JMSQueuePassword");
}

So far it was in activity (but not as static), so that I was possible to get package manager, but now i want to move this piece of code to another class which doesn't inherits ContextWrapper (from where we can get package manager). Is it possible somehow? This class is something like util class.

like image 458
mattis Avatar asked Jan 30 '26 01:01

mattis


1 Answers

You can pass the Context from the calling method to the method in the Util class and use the context there to get the details you want. This way you can call the method in the Util class from different modules in your application, with different contexts.

// Calling the Util method
Bundle metadata = Util.getMetaData(context);
...
// Inside the Util class
public static Bundle getMetaData(Context context) {
    return context.getPackageManager().getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA).metaData;
}
like image 105
Rahul Avatar answered Feb 01 '26 17:02

Rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!