您的位置:首页 > 移动开发 > Android开发

Android中Context简介

2012-07-02 21:25 260 查看
The way to do this is to create your own subclass of android.app.Application, and then specify that class in the application tag in your manifest. Now Android will automatically create an instance of that class and make it available for your entire application. You can access it from any context using the Context.getApplicationContext() method (Activity also provides a method getApplication() which has the exact same effect):

classMyAppextendsApplication{

privateString myState;

publicString getState(){
return myState;
}
publicvoid setState(String s){
myState = s;
}
}

classBlahextendsActivity{

@Override
publicvoid onCreate(Bundle b){
...
MyApp appState =((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: