[英]ApplicationInfo on Android in static method
Not that complicated of a question today: 今天没有那么复杂的问题:
Is there a way to get the ApplicationInfo in a static method? 有没有办法以静态方法获取ApplicationInfo?
No. 没有。
To get to ApplicationInfo
you need instance of Context
, which you get via and instance of Application
or Activity
. 要获得
ApplicationInfo
您需要Context
实例,您可以通过Context
实例以及Application
或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. this
是活动,服务或应用程序的上下文实例。
You can inherit your your application class from Application 您可以从Application继承您的应用程序类
public class TestApplication extends Application
create private static instance variable inside this class 在此类内创建私有静态实例变量
private static TestApplication instance = null;
instantiate it in onCreate method: 在onCreate方法中实例化它:
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. 用同样的方法可以获得ApplicationContext。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.