方法1:
通过不同Activity之间的消息传递机制实现数据共享
Activity1:
Intent intent=new intent(Activity1.this,Activity2.clas);
Bundle data=new data();
data.putString("name",name);
intent.putExtras(data);
StartActivity(intent);
Activity2:
Intent intent=getIntent();
Bundle data=intent.getExtras();
String name=data.getString("name);
方法2:
将需要共享的变量声明为public static类型
方法3:
新建Application的子类,在该类中声明共享的变量
class MyApp extends Application {
private String myState;
public String getState() {
return myState;
}
public void setState(String s) {
myState = s;
}
}
在AndroidManifest.xml的application加个name属性就可以了,如下面所示:
<application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name">
使用时:
class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
参考网址:
http://www.eoeandroid.com/thread-231275-1-1.html