在Android中传递数据的方法非常多,本次介绍4中比较常用的数据传递方法:
1.通过Intent传递数据
2.通过静态变量(static)传递数据
3.通过剪贴板(Clipboard)传递数据
4.通过全局对象传递数据
在TransmitDataActivity.java中
package mobile.android.transmit.data; public class TransmitDataActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button1=(Button)findViewById(R.id.button1); Button button2=(Button)findViewById(R.id.button2); Button button3=(Button)findViewById(R.id.button3); Button button4=(Button)findViewById(R.id.button4); button1.setOnClickListener(new ButtonListenner()); button2.setOnClickListener(new ButtonListenner()); button3.setOnClickListener(new ButtonListenner()); button4.setOnClickListener(new ButtonListenner()); } class ButtonListenner implements OnClickListener{ @SuppressLint("NewApi") @Override public void onClick(View view) { Intent intent=null; switch (view.getId()) { case R.id.button1: intent=new Intent(TransmitDataActivity.this,MyActivity1.class); //保存String类型的值 intent.putExtra("intent_string", "通过Intent传递的字符串"); //保存integer类型的值 intent.putExtra("intent_integer", 300); Data data=new Data(); data.id=1000; data.name="Android"; //保存可序列化的对象 intent.putExtra("intent_object", data); //显示用于接收数据的Activity startActivity(intent); break; case R.id.button2: intent=new Intent(); intent.setClass(TransmitDataActivity.this, MyActivity2.class); //下面的代码为MyActivity2中放入三个静态变量赋值 MyActivity2.id=3000; MyActivity2.name="保时捷"; MyActivity2.data=new Data(); MyActivity2.data.id=5555; MyActivity2.name="Android"; startActivity(intent); break; case R.id.button3: intent=new Intent(TransmitDataActivity.this,MyActivity3.class); //获取剪贴板对象(ClipboardManager) ClipboardManager clipboard=(ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); //创建Data对象 Data clipboardData=new Data(); //设置Data对象中字段的值 clipboardData.id=6666; clipboardData.name="通过Clipboard传递的数据"; //创建字节数组输出流对象,用于将Data对象转换为字节流 ByteArrayOutputStream baos=new ByteArrayOutputStream(); //用于保存Data对象生成Base64格式的字符串 String base64Str=""; try { ObjectOutputStream oos=new ObjectOutputStream(baos); //将Data对象写入对象输出流 oos.writeObject(clipboardData); //将字节流进行Base64编码 base64Str=Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT); oos.close(); } catch (IOException e) { e.printStackTrace(); } //获取存储文本数据的剪贴板数据对象(ClipData) ClipData clipData=ClipData.newPlainText("data", base64Str); //设置主剪贴板 clipboard.setPrimaryClip(clipData); //显示Myactivity3窗口 startActivity(intent); break; case R.id.button4: //获取MyApp对象 MyApp myApp=(MyApp) getApplicationContext(); myApp.country="美国"; myApp.data.id=1234; myApp.data.name="飞碟"; intent=new Intent(TransmitDataActivity.this,MyActivity4.class); startActivity(intent); break; default: break; } } } }
Data类:这个类是可序列化的,也就是实现了java.io.Serializable接口的类。
package mobile.android.transmit.data; import java.io.Serializable; public class Data implements Serializable{ public int id; public String name; }
在Myactivity1.java中:package mobile.android.transmit.data;
public class MyActivity1 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.myactivity); TextView textView=(TextView)findViewById(R.id.textview); //获得String类型的值 String intentString=getIntent().getStringExtra("intent_string"); //获得Integer类型的值 int intentInteger=getIntent().getExtras().getInt("intent_integer"); //获得Data类型的值 Data data=(Data) getIntent().getExtras().get("intent_object"); StringBuffer sb=new StringBuffer(); sb.append("Intent_string:"); sb.append(intentString); sb.append("\n"); sb.append("intent_integer:"); sb.append(intentInteger); sb.append("\n"); sb.append("data.id:"); sb.append(data.id); sb.append("\n"); sb.append("data.name:"); sb.append(data.name); sb.append("\n"); //在屏幕上输出传递过来的值 textView.setText(sb.toString()); } }
在Myactivity2.java中:
package mobile.android.transmit.data; public class MyActivity2 extends Activity{ public static String name; public static int id; public static Data data; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.myactivity); TextView textView=(TextView)findViewById(R.id.textview); StringBuffer sb=new StringBuffer(); sb.append("name:"); sb.append(name); sb.append("\n"); sb.append("id:"); sb.append(id); sb.append("\n"); sb.append("data.id:"); sb.append(data.id); sb.append("\n"); sb.append("data.name:"); sb.append(data.name); sb.append("\n"); textView.setText(sb.toString()); } }
在Myactivity3中:
package mobile.android.transmit.data; public class MyActivity3 extends Activity{ @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.myactivity); TextView textView=(TextView)findViewById(R.id.textview); //获取剪贴板对象 ClipboardManager clipboardManager=(ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); //从剪贴板中获取Base64编码字符串 String base64Str=clipboardManager.getPrimaryClip().getItemAt(0).getText().toString(); //将Base64编码字符串编码成字节数组 byte[] buffer=Base64.decode(base64Str, Base64.DEFAULT); ByteArrayInputStream bais=new ByteArrayInputStream(buffer); try { ObjectInputStream ois=new ObjectInputStream(bais); //将字节流还原成Data对象 Data data=(Data) ois.readObject(); //将Base64编码原文和Data对象字段值显示在TextView控件中 textView.setText(base64Str+"\n\ndata.id:"+data.id+"\ndata.name:"+data.name); } catch (Exception e) { e.printStackTrace(); } } }
在Myactivity4.java中:
package mobile.android.transmit.data; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MyActivity4 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.myactivity); TextView textView=(TextView) findViewById(R.id.textview); MyApp myApp=(MyApp) getApplicationContext(); textView.setText("MyApp.country:"+myApp.country+"\nMyApp.data.id:"+myApp.data.id+"\nMyApp.data.name:"+myApp.data.name); } }
关于在不同Activity之间传递数据的四种常用方法小编就给大家介绍这么多,希望对大家有所帮助!