Android uses a clipboard to deliver data

Source: Internet
Author: User
Tags base64

One of the problems we often encounter in Android development is the sharing of data between different activity. There are many ways to reach this goal in Android development.

Here is a more common and commonly used method is to use the shear plate. We've all worked with copy on Windows and Linux. Here's what this is about.

Invoke Service

[Java]View Plaincopy
    1. Clipboardmanager Clipboardmanager = (clipboardmanager) getsystemservice (Context.clipboard_service);

It is important to note that before Android version 11, the use of the Clipboard to pass data using the SetText and GetText methods, but after version 11, the two GetText and set methods are discarded, instead of the need to use the Clipdata object to pass the data. Compared with the GetText and SetText methods, the use of Clipdata objects to pass data, more consistent with object-oriented programming thinking, and the type of data can be passed more. For a description of the Clipdata object, see the Clipdata object description.

This is about passing strings using Clipdata.

1. Write values to the Clipboard

[Java]View Plaincopy
    1. clipdata cd = Clipdata.newplaintext ("label", basetostring);
    2. Clipboardmanager.setprimaryclip (CD);

2. Read the values of the Clipboard

[Java]View Plaincopy
    1. if (Cm.getprimaryclipdescription (). Hasmimetype (Clipdescription.mimetype_text_plain))
    2. {
    3. Clipdata cd = Cm.getprimaryclip ();
    4. Item item = Cd.getitemat (0);
    5. TxtView.setTextitem.getText (). toString ());
    6. }

This makes it possible to use the Clipboard to pass the data.
What needs to be explained here is that in real programming, if you really need to use the Clipboard to pass the value, you need to judge the version of the Android system before invoking the service.

[Java]View Plaincopy
    1. Build.version.sdk_int>=build.version_codes. Honeycomb

If you need to use custom objects to pass values. When using the Clipboard, the object must be converted. Here we need to use the Base64 tool class. For the usage of this class, see the Base64 class.

1. Read the custom object and convert it to a string, writing to the Clipboard

[Java]View Plaincopy
  1. //Customize an object   
  2. MyData MD = new MyData ("KIMIFDW",+);
  3. //define string   
  4. String basetostring ="";
  5. //1. Converting an object to a string   
  6. Bytearrayoutputstream Bytearrayoutputstream = new bytearrayoutputstream ();
  7. Try   
  8. {
  9. ObjectOutputStream ObjectOutputStream = new ObjectOutputStream (Bytearrayoutputstream);
  10. Objectoutputstream.writeobject (MD);
  11. basetostring = base64.encodetostring (Bytearrayoutputstream.tobytearray (), base64.default);
  12. Objectoutputstream.close ();
  13. }
  14. Catch (Exception e)
  15. {
  16. E.printstacktrace ();
  17. }

2. Read the Clipboard string and convert it to an object

[Java]View Plaincopy
  1. //Convert base64 to ToString   
  2. byte  [] base64tostring = Base64.decode (Item.gettext (). toString (), base64.default);
  3. //Read data from the stream   
  4. Bytearrayinputstream Bytearrayinputstream = new Bytearrayinputstream (base64tostring);
  5. Try   
  6. {
  7. ObjectInputStream ObjectInputStream = new ObjectInputStream (Bytearrayinputstream);
  8. MyData MD = (MyData) objectinputstream.readobject ();
  9. Objectinputstream.close ();
  10. Txtview.settext (Md.tostring ());
  11. }
  12. Catch (Exception e)
  13. {
  14. E.printstacktrace ();
  15. }

Here, the main use of the IO in the operation of some streams. It is important to note that both the read stream and the write stream must be closed after it is exhausted

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.