简体   繁体   English

使用getApplicationContext()与在Android中引用自定义Application类

[英]Using getApplicationContext() vs. referencing to custom Application class in Android

I've been researching ways to store global settings for my Android application and so far the best way seems to extend the Application class and store the shared data inside it, as described here . 我一直在研究如何存储的全局设置为我的Android应用程序,到目前为止最好的办法似乎扩展应用类和共享数据存储在它里面,描述在这里 I've discovered that instead of using (CustomApplicationClass)getApplicationContext().getSomething() i can do the same thing by referencing directly to the static method inside the class like this: CustomApplicationClass.getSomething() and both ways work just fine. 我发现不用使用(CustomApplicationClass)getApplicationContext().getSomething()我可以通过直接引用类内部的静态方法来做同样的事情: CustomApplicationClass.getSomething() ,两种方法都可以正常工作。

Here's a piece from CustomApplicationClass: 这是CustomApplicationClass的一部分:

public class CustomApplicationClass extends Application {

  private static boolean something;

  @Override
  public void onCreate() {
    [...]
  }

  public static boolean isSomething() {
    return something;
  }

  public static void setSomething(boolean something) {
    this.something = something;
  }

}

Now, if i want to retrieve value of "something" variable somewhere in my code, say, from my application Activity, is there a difference between: 现在,如果我想在我的代码中某处检索“某物”变量的值,例如,从我的应用程序“活动”中获取以下内容之间的区别:

boolean var1 = ((CustomApplicationClass)getApplicationContext()).isSomething();

and

boolean var1 = CustomApplicationClass.isSomething();

? When running the application, both work fine. 运行该应用程序时,两者都可以正常工作。 Is the second way safe to use, or is it inadvisable? 第二种方法使用安全吗?还是不建议这样做?

I've been researching ways to store global settings for my Android application and so far the best way seems to extend the Application class and store the shared data inside it, as described here. 我一直在研究为Android应用程序存储全局设置的方法,到目前为止,最好的方法似乎是扩展Application类并在其中存储共享数据,如此处所述。

Except that you're not doing that. 除非您没有这样做。

I've discovered that instead of using (CustomApplicationClass)getApplicationContext().getSomething() i can do the same thing by referencing directly to the static method inside the class like this: CustomApplicationClass.getSomething() and both ways work just fine. 我发现不用使用(CustomApplicationClass)getApplicationContext()。getSomething(),我可以通过直接引用类内部的静态方法来做同样的事情:CustomApplicationClass.getSomething(),两种方法都可以正常工作。

Of course. 当然。 You could just as easily had CustomApplicationClass extend Object , then executed CustomApplicationClass.getSomething() . 您可以轻松地让CustomApplicationClass扩展Object ,然后执行CustomApplicationClass.getSomething() You are gaining nothing by your current approach versus just using an ordinary singleton pattern in Java, and you are losing flexibility, as an application can only have one custom subclass of Application . 与仅在Java中使用普通的单例模式相比,通过当前的方法没有获得任何收益,并且失去了灵活性,因为一个应用程序只能具有Application一个自定义子类。

Is the second way safe to use, or is it inadvisable? 第二种方法使用安全吗?还是不建议这样做?

The first way is pointless, since your data member and methods are static. 第一种方法没有意义,因为您的数据成员和方法是静态的。

Either: 要么:

  1. Make your stuff in CustomApplicationClass not be static , and then use getApplicationContext() . 使CustomApplicationClass 不是 static ,然后使用getApplicationContext()

  2. Refactor CustomApplicationClass to not extend Application , and then use the static data member and/or accessor methods, or switch more formally to the Java singleton pattern . 重构CustomApplicationClass以不扩展Application ,然后使用静态数据成员和/或访问器方法,或者更正式地切换到Java单例模式

Personally, I would go with option #2. 就个人而言,我会选择选项2。

If you check the api of android.app.Application ( http://developer.android.com/reference/android/app/Application.html ) then you will find on Class Overview as following: 如果您检查android.app.Application的api( http://developer.android.com/reference/android/app/Application.html ),那么您会在类概述中找到以下内容:

Base class for those who need to maintain global application state. 需要维护全局应用程序状态的人员的基类。 You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created. 您可以通过在AndroidManifest.xml的标签中指定其名称来提供自己的实现,这将在创建应用程序/包的过程时为您实例化该类。

There is normally no need to subclass Application. 通常不需要子类化Application。 In most situation, static singletons can provide the same functionality in a more modular way. 在大多数情况下,静态单例可以以更模块化的方式提供相同的功能。 If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton. 如果您的单身人士需要全局上下文(例如,注册广播接收者),则可以在首次构造单身人士时为该函数提供一个上下文,该上下文在内部使用Context.getApplicationContext()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 getApplication()与getApplicationContext() - getApplication() vs. getApplicationContext() 如何初始化Application类并在android中存储getApplicationContext()? - How to initialize an Application class and store getApplicationContext() in android? 什么时候调用Application类的getApplicationContext()? - When is getApplicationContext() of Application class called? 通过Android在单例类中的getApplicationContext()? - getApplicationContext() in a singleton class via android? android:使用getApplicationContext时ListView中的白色textColor - android: White textColor in ListView when using getApplicationContext 在另一个简单的类中而不是在MainActivity.java中使用getApplicationContext() - Using getApplicationContext() in another simple class, NOT in MainActivity.java 使用Android的getBaseContext,getApplicationContext或使用Activity的“this”的不同规则 - Diffinitive rules for using Android's getBaseContext, getApplicationContext or using an Activity's “this” 使用getApplicationContext将上下文传递给标准Java类会导致强制关闭 - Passing context to a standard java class using getApplicationContext causes Force Close Android getApplicationContext() 对空对象引用 - Android getApplicationContext() on a null object reference 从ReceiverRestrictedContext获得应用程序,就像使用了getApplication一样(不是getApplicationContext) - Obtain application from ReceiverRestrictedContext as if getApplication was used (not getApplicationContext)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM