1. 使用Intent来传递数据(谁都会用)
2. 在Activity之间使用静态变量传递数据(可以用来传递对象)
Intent intent = new Intent();
intent.setClass(IntentDemo.this, Other.class);
Other.age = 22;
Other.name = "AHuier";
Other.address = "XiaMen";
startActivity(intent);
//Other.java
public static int age;
public static String name;
public static String address;
3. 通剪切板传递数据(不局限于app内,其他的任何程序都可以从剪切板中获取数据)
//写入
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
String name = "AHuier";
clipboardManager.setText(name);
//读取
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
String msgString = clipboardManager.getText().toString();
4. 使用全局变量来传递数据(通过可以共同访问的区域为中介,进行读写)
//备注
public class MyApp extends Application {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setName("AHuier");
}
}
//写入
myApp = (MyApp)getApplication();
myApp.setName("kunhuixu"); //修改之后的名称
//读取
myApp = (MyApp)getApplication();
textView.setText("--- The app name ---" + myApp.getName());
父文链接