分类: Java
2011-11-23 14:35:23
新建一个继承 android.app.Application的的子类,然后在manifest的application标签指定该类。android 会自动实例化该类,使得整个应用都可用使用该类的对象。可用通过Context.getApplicationContext()和Activity提供的getApplication()方法获得该实例化对象。
demo:
class MyApp extends Application{ 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(); ... } }在AndroidManifest.xml application 标签中添加 android:name