Android中Context简介

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();
   
...
 
}
}
posted @ 2012-07-02 21:25  carbs  阅读(154)  评论(0编辑  收藏  举报