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()即可