mainActivity.java:
1 package com.example.intent3; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.ObjectOutputStream; 5 import android.app.Activity; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.text.ClipboardManager; 10 import android.util.Base64; 11 import android.view.View; 12 import android.widget.Button; 13 14 @SuppressWarnings("deprecation") 15 public class MainActivity extends Activity { 16 /** Called when the activity is first created. */ 17 private Button button; 18 19 @Override 20 public void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 button = (Button) this.findViewById(R.id.button); 24 button.setOnClickListener(new View.OnClickListener() { 25 @Override 26 public void onClick(View v) { 27 28 // 从android系统中调用剪切板的服务 29 // ClipboardManager clipboardManager = (ClipboardManager) 30 // getSystemService(Context.CLIPBOARD_SERVICE); 31 // String name = "jack"; 32 // clipboardManager.setText(name); 33 // Intent intent = new Intent(Main.this, OtherActivity.class); 34 // startActivity(intent); 35 MyData myData = new MyData("jack", 9); 36 // 将对象转换成字符串 37 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 38 String base64String = ""; 39 try { 40 ObjectOutputStream objectOutputStream = new ObjectOutputStream( 41 byteArrayOutputStream); 42 objectOutputStream.writeObject(myData); 43 base64String = Base64.encodeToString( 44 byteArrayOutputStream.toByteArray(), Base64.DEFAULT); 45 objectOutputStream.close(); 46 } catch (Exception e) { 47 // TODO: handle exception 48 } 49 ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 50 clipboardManager.setText(base64String); 51 Intent intent = new Intent(MainActivity.this, Other.class); 52 startActivity(intent); 53 } 54 }); 55 } 56 }
OtheAactivity.java:
package com.example.intent3; import java.io.ByteArrayInputStream; import java.io.ObjectInputStream; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.text.ClipboardManager; import android.util.Base64; import android.widget.TextView; @SuppressWarnings("deprecation") public class Other extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); textView = (TextView) this.findViewById(R.id.msg); ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String msgString = clipboardManager.getText().toString(); byte[] base64_byte = Base64.decode(msgString, Base64.DEFAULT); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( base64_byte); try { ObjectInputStream objectInputStream = new ObjectInputStream( byteArrayInputStream); MyData myData = (MyData) objectInputStream.readObject(); textView.setText(myData.toString()); } catch (Exception e) { // TODO: handle exception } } }
MyData.java:
package com.example.intent3; import java.io.Serializable; public class MyData implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; public MyData(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "MyData [name=" + name + ", age=" + age + "]"; } }