Android uses the Clipboard to pass data

Source: Internet
Author: User

Using the Clipboard to pass data, you can pass simple data, or you can pass a serializable object.

Let's start with a simple point.

First, add a button to the Mainactivity.xml file.

Privatebutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //TODO auto-generated Method StubButton = (Button) This. Findviewbyid (R.id.button); Button.setonclicklistener (NewView.onclicklistener () {@SuppressLint ("Newapi") @Override Public voidOnClick (View v) {//TODO auto-generated Method Stub//first case: Clipboard passes common type data//Clipboardmanager Clipboardmanager = (clipboardmanager) getsystemservice (Context.clipboard_servi CE);//calling the Clipboard service from the Android system//String name = "Jack";//Clipboardmanager.settext (name);//Intent Intent = new Intent (mainactivity.this,otheractivity.class);//startactivity (Intent); //second case: Clipboard transfer serializable objectMyData MyData =NewMyData ("Jack","Beijing"); //Convert an object to a stringBytearrayoutputstream Bytearrayoutputstream =NewBytearrayoutputstream (); String basestring=""; Try{ObjectOutputStream ObjectOutputStream=NewObjectOutputStream (Bytearrayoutputstream);                            Objectoutputstream.writeobject (MyData); Basestring=base64.encodetostring (Bytearrayoutputstream.tobytearray (), base64.default);                        Objectoutputstream.close (); } Catch(Exception e) {//Todo:handle Exception} clipboardmanager Clipboardmanager=(Clipboardmanager) Getsystemservice (Context.clipboard_service);                        Clipboardmanager.settext (basestring); Intent Intent=NewIntent (mainactivity. This, Otheractivity.class);                    StartActivity (Intent);            }                }); }

Before this, of course, you need to create a new object, new Mydata.java

Package Com.example.android_intent3;import java.io.Serializable; Public classMyData implements Serializable {PrivateString age; PrivateString name;  PublicMyData (string age, String name) {super ();  This. Age =Age ;  This. Name =name; } @Override PublicString toString () {return "MyData [age="+ Age +", Name="+ name +"]"; }     PublicString getage () {returnAge ; }     Public voidsetage (String age) { This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicMyData () {//TODO auto-generated Constructor stub    }}

Of course, you also need to add a button to the Mainactivity.xml file

<button        android:id="@+id/button"        android:layout_width= " fill_parent "         android:layout_height="wrap_content"        Android:text =" pass data with clipboard " />

You also need to add a new layout file to the Other.xml file:

<textview android:id="@+id/msg"  android:layout_width="Fill_ Parent"    android:layout_height="fill_parent"></ Textview>

Yes, you have to create a new Otheractivity.java file, add:

PrivateTextView TextView;  Publicotheractivity () {//TODO auto-generated Constructor stub} @SuppressLint ("Newapi") @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (savedinstancestate);        Setcontentview (R.layout.other); //first Case//TextView = (textView) This.findviewbyid (r.id.msg);//Clipboardmanager Clipboardmanager = (clipboardmanager) getsystemservice (context.clipboard_service);//String msgstring = Clipboardmanager.gettext (). toString (); //Textview.settext (msgstring); //The second caseTextView = (TextView) This. Findviewbyid (r.id.msg); //get Android Clipboard serviceClipboardmanager Clipboardmanager =(Clipboardmanager) Getsystemservice (Context.clipboard_service); //get the data in the ClipboardString msgstring =Clipboardmanager.gettext (). toString (); //decoding        byte[] Base64_byte =Base64.decode (msgstring, Base64.default); //Output DataBytearrayinputstream Bytearrayinputstream =NewBytearrayinputstream (Base64_byte); Try{ObjectInputStream ObjectInputStream=NewObjectInputStream (Bytearrayinputstream); MyData MyData=(MyData) objectinputstream.readobject ();        Textview.settext (Mydata.tostring ()); } Catch(Exception e) {//Todo:handle Exception        }    }

Finally, of course you can't forget to add Otheractivity.java to the manifest file.

<activity android:name=". Otheractivity"></activity>

Android uses the Clipboard to pass data

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.