How Android uses the Clipboard to pass simple data and complex data

Source: Internet
Author: User

The scene that passes the data is to jump between different pages, need to carry the data: simple data value refers to a string, int and other data, complex data refers to the class

1. Use the Clipboard to pass a simple data method:

The first page inside the data operation is as follows:

1   Clipboardmanager Clipboardmanager = (Clipboardmanager); 2   Getsystemservice (context.clipboard_service);          3   String Text = "simple data"; 4   clipboardmanager.settext (text); 5   New Intent (This, otheractivity.  Class); 6   StartActivity (Intent);

The second page takes data operations as follows:

  Intent Intent = Getintent ();  TextView = Findviewbyid (r.id.msgtext);  MyApp = (MyApp) getapplication ();  Textview.settext ("After changed:" + Myapp.gettext ());            

Where MyApp is a class file with the following:

public class MyApp extends application {    private String text;    Public String GetText () {        return text;    }    public void SetText (String text) {        this.text = text;    }}

Add the MyApp class to the manifest file:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "    package=" Com.example.xxx.globalvariables ">    <application        android:name=". MyApp "        android:allowbackup=" true "        android:icon=" @mipmap/ic_launcher "        android:label=" @string/ App_name "        android:roundicon=" @mipmap/ic_launcher_round "        android:supportsrtl=" true "        android:theme=" @style/apptheme ">        <activity android:name=". Mainactivity ">            <intent-filter>                <action android:name=" Android.intent.action.MAIN "/>                <category android:name= "Android.intent.category.LAUNCHER"/>            </intent-filter>        </ activity>        <activity android:name= ". Otheractivity "></activity>    </application></manifest>

2. Using the Clipboard to pass a complex data transfer method

The first page to store data is as follows:

Method Two: Clipboard passes complex data        MyData MyData = new MyData ("Jack");        Converts an object to a string        bytearrayoutputstream Bytearrayoutputstream = new Bytearrayoutputstream ();        String base64string = "";        try {            ObjectOutputStream objectoutputstream = new ObjectOutputStream (bytearrayoutputstream);            Objectoutputstream.writeobject (myData);            base64string = base64.encodetostring (Bytearrayoutputstream.tobytearray (), base64.default); Encrypt            objectoutputstream.close ();        } catch (IOException e) {            e.printstacktrace ();        }        Clipboardmanager clipboardManager1 = (clipboardmanager) getsystemservice (context.clipboard_service);        Clipboardmanager1.settext (base64string);        Intent intent1 = new Intent (this, otheractivity.class);        StartActivity (INTENT1);

The second page is the method of fetching data:

            Intent Intent = Getintent ();

Clipboardmanager Clipboardmanager = (clipboardmanager) getsystemservice (clipboard_service); String msg = Clipboardmanager.gettext (). toString (); Clipboardtextview = Findviewbyid (R.id.clipboardmsgtext); Decode byte[] base64_byte = Base64.decode (msg, base64.default); Bytearrayinputstream Bytearrayinputstream = new Bytearrayinputstream (base64_byte); ObjectInputStream objectinputstream = null; try { ObjectInputStream = new ObjectInputStream (bytearrayinputstream); MyData MyData = (MyData) objectinputstream.readobject (); Clipboardtextview.settext (Mydata.tostring ()); } catch (Exception e) { e.printstacktrace (); }

MyData is a class that includes the name, the Get method for age, and the ToString () method, which needs to implement

Serializable

Note: Because the class is a normal class, there is no inheritance
Application, so no more configuration in manifest file!

How Android uses the Clipboard to pass simple data and complex data

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.