全局获取Context

1、定制一个Application类,管理全局的状态信息

public class MyApplication extends Application{
    private static Context context;
    @Override
    public void onCreate(){
        context = getApplicationContext();
    }
    public static Context getContext(){
        return context;
    }
}

2、在AndroidManifest.xml中指定自定义的Application

<application

  android:name = "com.exmple.networktest.MyApplication"

  ......>

</application>

3、当要获取Context时,调用MyApplication.getContext()即可

posted @ 2017-02-22 21:36  yl007  阅读(198)  评论(0编辑  收藏  举报