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
- 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
- clipdata cd = Clipdata.newplaintext ("label", basetostring);
- Clipboardmanager.setprimaryclip (CD);
2. Read the values of the Clipboard
[Java]View Plaincopy
- if (Cm.getprimaryclipdescription (). Hasmimetype (Clipdescription.mimetype_text_plain))
- {
- Clipdata cd = Cm.getprimaryclip ();
- Item item = Cd.getitemat (0);
- TxtView.setTextitem.getText (). toString ());
- }
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
- 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
- //Customize an object
- MyData MD = new MyData ("KIMIFDW",+);
- //define string
- String basetostring ="";
- //1. Converting an object to a string
- Bytearrayoutputstream Bytearrayoutputstream = new bytearrayoutputstream ();
- Try
- {
- ObjectOutputStream ObjectOutputStream = new ObjectOutputStream (Bytearrayoutputstream);
- Objectoutputstream.writeobject (MD);
- basetostring = base64.encodetostring (Bytearrayoutputstream.tobytearray (), base64.default);
- Objectoutputstream.close ();
- }
- Catch (Exception e)
- {
- E.printstacktrace ();
- }
2. Read the Clipboard string and convert it to an object
[Java]View Plaincopy
- //Convert base64 to ToString
- byte [] base64tostring = Base64.decode (Item.gettext (). toString (), base64.default);
- //Read data from the stream
- Bytearrayinputstream Bytearrayinputstream = new Bytearrayinputstream (base64tostring);
- Try
- {
- ObjectInputStream ObjectInputStream = new ObjectInputStream (Bytearrayinputstream);
- MyData MD = (MyData) objectinputstream.readobject ();
- Objectinputstream.close ();
- Txtview.settext (Md.tostring ());
- }
- Catch (Exception e)
- {
- E.printstacktrace ();
- }
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