Android Learning Notes--using the Clipboard to pass values in the Activity sample code _android

Source: Internet
Author: User
Tags base64 gettext stub
Passing data between activity can also take advantage of the ability to support a technology called a clipboard, whether Windows or Linux operating systems, where a program copies some data to the Clipboard, and then any other program can get data from the Clipboard. This technology also exists in the Android system.

The use of the Clipboard will use the Clipboardmanager object, which is used by the Clipboard, and the Clipboardmanager image is used to manipulate the Clipboard, but does not provide a public constructor (a single case pattern). You need to use Activity.getsystemservice (Context.clipboard_service) to get the object.

Prior to the ANDROID-11 (Android 3.0) version, the SetText () and GetText () methods were used to pass data using the Clipboard, but after this release, the two methods were discarded, instead of passing Clipdata objects. In contrast to GetText and SetText, the use of Clipdata objects to pass data is more consistent with object-oriented thinking, and the types of data that can be passed are diverse.

Main steps:
Obtains the Clipboardmanager object cm by Getsystemservice.
Use the Cm.setprimaryclip () method to set the Clipdata data object.
Gets the Clipboardmanager object cm in the new activity.
Use the Cm.getprimaryclip () method to get the Clipdata data object for the Clipboard, CD.
The data passed in is obtained through Cd.getitemat (0).

Sample code
Save data:
Copy Code code as follows:

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Button btn= (button) Findviewbyid (R.ID.BTN);
Btn.setonclicklistener (New View.onclicklistener () {
@SuppressLint ("Newapi")
@Override
public void OnClick (View v) {
Get the Clipboard
Clipboardmanager cm= (Clipboardmanager) Getsystemservice (Context.clipboard_service);
Cm.setprimaryclip (Clipdata.newplaintext ("Data", "Jack"));
Intent intent=new Intent (mainactivity.this,otheractivity.class);
StartActivity (Intent);
}
});
}

Read data:
Copy Code code as follows:

protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.other);
Clipboardmanager cm= (Clipboardmanager) Getsystemservice (Context.clipboard_service);
Clipdata Cd=cm.getprimaryclip ();
String Msg=cd.getitemat (0). GetText (). toString ();
TextView tv= (TextView) Findviewbyid (r.id.msg);
Tv.settext (msg);
}

The above method uses the Clipboard to pass data of type string, and if an object needs to be passed, the object being passed must be serializable, and serialization is marked by implementing the Serializable interface.
Main steps:
Create a class MyData that implements the serializable interface.
Save data: Gets the Clipboardmanager, serializes the MyData object through the Base64 class, and then deposits it into the clipboard.
Take out the data: in the new activity, get Clipboardmanager, deserialize the serialized data, and use the Base64 class. The deserialized data is then processed.
Sample code:
Step One:
Copy Code code as follows:

public class MyData implements Serializable {
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;
}
}

Step Two:
Copy Code code as follows:

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Button btn= (button) Findviewbyid (R.ID.BTN);
Btn.setonclicklistener (New View.onclicklistener () {
@SuppressLint ("Newapi")
@Override
public void OnClick (View v) {
Get the Clipboard
Clipboardmanager cm= (Clipboardmanager) Getsystemservice (Context.clipboard_service);
MyData mydata=new MyData ("Jack", 24);
String basetostring= "";
Bytearrayoutputstream barr=new Bytearrayoutputstream ();
Try
{
ObjectOutputStream oos=new ObjectOutputStream (BARR);
Oos.writeobject (MyData);
Basetostring=base64.encodetostring (Barr.tobytearray (), base64.default);
Oos.close ();
}
catch (Exception e)
{
E.printstacktrace ();
}
Cm.setprimaryclip (Clipdata.newplaintext ("Data", basetostring));
Intent intent=new Intent (mainactivity.this,otheractivity.class);
StartActivity (Intent);
}
});
}

Step Three:
Copy Code code as follows:

protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.other);
Clipboardmanager cm= (Clipboardmanager) Getsystemservice (Context.clipboard_service);
Clipdata Cd=cm.getprimaryclip ();
String Msg=cd.getitemat (0). GetText (). toString ();
Byte[] Base64_btye=base64.decode (msg, base64.default);
Bytearrayinputstream bais=new Bytearrayinputstream (Base64_btye);
try {
ObjectInputStream ois=new ObjectInputStream (Bais);
MyData mydata= (MyData) ois.readobject ();
TextView tv= (TextView) Findviewbyid (r.id.msg);
Tv.settext (Mydata.tostring ());
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

Summarize
To sum up, using the Clipboard to pass data both pros and cons, the Clipboard is managed by the Android system, so the data stored in one place is accessible to any application on this Android device, but precisely because the device accesses the same clipboard, it can cause data to be stored in the current program. Overwritten by other programs before use, resulting in no guarantee of proper data acquisition.
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.