Android全局变量

public class MyApp extends Application {  
      
      private String myState;  
      
      public String getState(){  
        return myState;  
      }  
      public void setState(String s){  
        myState = s;  
      }  
    }  
      
public class Blah extends Activity {  
      
      @Override  
      public void onCreate(Bundle b){  
        ...  
        MyApp appState = ((MyApp)getApplicationContext());  
        String state = appState.getState();  
        ...  
      }  
    }

然后再manifest中添加应用:

<application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name">  
            <activity android:name=".ClickableListItemActivity"  
                      android:label="@string/app_name">  
                <intent-filter>  
                    <action android:name="android.intent.action.MAIN" />  
                    <category android:name="android.intent.category.LAUNCHER" />  
                </intent-filter>  
            </activity>  
      
        </application>

说明:

需添加的内容:android:name=".your_App_Name"

位置:当前activity所在的位置

相关推荐