在不同的Activity 之间传递参数主要有四种方式:
1. 通过Intent 传递数据
这是最为常用的参数传递方式,但是被传递的字段或者类都必须实现serializabel 接口
public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); switch (id) { case R.id.action_new : Toast.makeText(this, "create a new file", Toast.LENGTH_SHORT).show(); Intent msgIntent = new Intent(this, MessageActivity.class); /** * 使用Intent传递数据:方案一 * 前提和局限性: 传递的参数对象必须实现了序列化 serializable interface */ msgIntent.putExtra("flag", true); msgIntent.putExtra("intentName", "MsgIntent"); SimpleData simpleData = new SimpleData(199938L, "Test Data"); msgIntent.putExtra("simpleData", simpleData); startActivity(msgIntent); break; case R.id.action_save : Toast.makeText(this, "save the file", Toast.LENGTH_SHORT).show(); break; case R.id.action_settings : Toast.makeText(this, "The whole setting", Toast.LENGTH_SHORT).show(); break; case R.id.action_about : Toast.makeText(this, "show the about message", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(this, "do nothing", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }
方法中使用的JavaBean
/** * */ package org.navis.heard.bean; import java.io.Serializable; /** * Intent 之间传递参数时用到 的 javabean , * 局限性为:必须实现了序列化接口 * @author navis * */ public class SimpleData implements Serializable { /** * SerialVersionUID */ private static final long serialVersionUID = -2643114286876340652L; private long id; private String dataName; public SimpleData() { super(); } public SimpleData(long id, String dataName) { super(); this.id = id; this.dataName = dataName; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getDataName() { return dataName; } public void setDataName(String dataName) { this.dataName = dataName; } public static long getSerialversionuid() { return serialVersionUID; } @Override public String toString() { return "SimpleData [id=" + id + ", dataName=" + dataName + "]"; } }
2. 使用静态变量传递数据
使用静态变量和JavaEE 中使用的静态变量的方式相同,但是如果项目中使用大量的静态变量会使项目难于维护。
3. 通过剪贴板(Cilpboard)传递数据
剪切板只能存储简单类型数据或者可序列化的对象,对于某些不可序列化的对象如果可以将其转换成字节流(字节数组),也可以将这些对象保存到剪贴板中。
传输对象
/** * @author navis * */ private class Data{ private int id; private String name; public Data(int id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "Data [id=" + id + ", name=" + name + "]"; } }
将对象存储到剪贴板中
Data data = new Data(222, "222"); //获取系统剪贴板对象 ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); //通过流 将对象写入流 并对字节流进行加密 ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); String base64Str = ""; try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(arrayOutputStream); //将data对象写入对象输出流 objectOutputStream.writeObject(data); //将对象字节流进行编码 base64Str = Base64.encodeToString(arrayOutputStream.toByteArray(), Base64.DEFAULT); objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } //获取存储数据的剪贴板数据对象 ClipData clipData = ClipData.newPlainText("data", base64Str); //设置主剪贴板 clipboardManager.setPrimaryClip(clipData);
通过剪贴板获取对象
//从剪贴板中获取对象 //获取系统剪贴板对象 ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); //获取编码后的字符串 String resultString = clipboardManager.getPrimaryClip() .getItemAt(0).getText().toString(); byte[] buffer = Base64.decode(resultString, Base64.DEFAULT); ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(buffer); ObjectInputStream objectInputStream; try { objectInputStream = new ObjectInputStream(arrayInputStream); Data outData = (Data) objectInputStream.readObject(); outData.toString(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); }
4. 通过全局对象传递数据