Android transmits object data through the clipboard

Source: Internet
Author: User

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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.