Package com. example. androidtest; import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. objectInputStream; import java. io. objectOutputStream; import android. app. activity; import android. text. clipboardManager; import android. content. context; import android. OS. bundle; import android. util. base64; import android. view. menu; import android. view. view; import android. widget. textView; public class MainActivity extends Activity {private TextView TV; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TV = (TextView) findViewById (R. id. mytv);} public void onclick (View view) {MyData data = new MyData ("xi", "23"); String base64code = ""; try {// encode and encrypt ByteArrayOutputStream bytearray = new ByteArrayOutputStream (); ObjectOutputStream objectout = new ObjectOutputStream (bytearray); objectout. writeObject (data); base64code = Base64.encodeToString (bytearray. toByteArray (), Base64.DEFAULT); objectout. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} ClipboardManager clip = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE); clip. setText (base64code); getclip () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. activity_main, menu); return true;} public void getclip () {ClipboardManager clip = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE); String msg = clip. getText (). toString (); MyData mydata = null; // decodes byte [] bytearray = Base64.decode (msg, Base64.DEFAULT); ByteArrayInputStream byteinput = new ByteArrayInputStream (bytearray ); try {ObjectInputStream out = new ObjectInputStream (byteinput); mydata = (MyData) out. readObject ();} catch (Exception e) {// TODO: handle exception} TV. setText (mydata. toString ());}}
MyData class
package com.example.androidtest;import java.io.Serializable;public class MyData implements Serializable {private String name;private String age;public MyData(String n,String age){this.name = n;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn "name:"+name + " age:"+age;}}
Easy to troubleshoot:
The clipboard imports android. text. ClipboardManager; not android. content. ClipboardManager;