Android应用程序使之用总结

  

<强> android使用Application 配置全局上下文步骤:

,

1,写一个全局的单例模式的类BaseApplication继承自应用程序并覆盖onCreate()方法,在这个方法里面实例化应用程序;

& lt; Application  android: name=" com.xxx.android.common.BaseApplication,祝辞& lt;/application>

3,调用,根据类。方法名的即可获取背景。

,

,具体如下:

1,首先定义BaseApplication , extends 应用程序,如下

public  class  BaseApplication , extends 应用程序{   ,   ,,,public  void  onCreate ()   ,,{   ,,,,super.onCreate ();   ,,,,mContext =,;   ,,}   ,,   ,,public  static  Context  getContext ()   ,,{   ,,,return  mContext;   ,,}   ,,   ,,public  static  BaseApplication  getInstance ()   ,,{   ,,,,return  mContext;   以前,,}

,

2,在Mainfest.xml文件中应用的标签中加名字上属性,例如:& lt; application  android: name=" com.xxx.android.common.BaseApplication ",祝辞& lt;/application>

,

3,调用即可获取mContext(这里BaseApplication继承应用程序,所以会自动执行onCreate()方法):

BaseApplication.getContext ();

,

,

,

Android应用程序使之用总结