简体   繁体   English

Android上的ApplicationInfo静态方法

[英]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实例以及ApplicationActivity实例获得。

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.

相关问题 找不到方法android.content.pm.ApplicationInfo.loadLogo - Could not find method android.content.pm.ApplicationInfo.loadLogo Android ApplicationInfo是游戏吗? - Android ApplicationInfo is a game? Android中“设置”页面的ApplicationInfo - ApplicationInfo for Settings pages in android RecyclerView:尝试调用虚拟方法 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' - RecyclerView: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' java.lang.NullPointerException:尝试调用虚拟方法'android.content.pm.ApplicationInfo - java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo 问题:尝试在 null object 引用上调用虚拟方法“android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()” - Problem: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference Android - 从ApplicationInfo实例启动应用程序 - Android - Starting an application from an ApplicationInfo instance 如何通过android中的应用程序uid获取ApplicationInfo? - How to get ApplicationInfo by application uid in android? Android:applicationInfo.loadDescription()始终返回null - Android: applicationInfo.loadDescription() always returning null android - 静态方法问题 - android - issue with static method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM