ApplicationInfo on Android in static method
Not that complicated of a question today:
Is there a way to get the A开发者_运维技巧pplicationInfo in a static method?
No.
To get to ApplicationInfo
you need instance of Context
, which you get via and instance of Application
or Activity
.
So, normally you'd do:
ApplicationInfo appIngo = this.getPackageManager().getApplicationInfo("your.app.package.name", GET_META_DATA)
where this
is and instance of Context, either Activity, Service or Application.
You can inherit your your application class from Application
public class TestApplication extends Application
create private static instance variable inside this class
private static TestApplication instance = null;
instantiate it in onCreate method:
instance = this;
write access method
public static TestApplication getInstance() {
return instance;
}
From any place you can call
TestApplication.getInstance().getApplicationInfo()
Same way you can get ApplicationContext.
精彩评论