Some techniques can be used to pass data between activities. Whether it is Windows or Linux operating systems, it will support a technology called clipboard, that is, a program copies some data to the clipboard, and then any other program can obtain data from the clipboard. This technology also exists in the Android system.
The ClipboardManager object will be used when using a clipboard. The ClipboardManager object is used when using a clipboard. The ClipboardManager object is used to operate the clipboard, but does not provide a public constructor (singleton mode). You need to use (Context.CLIPBOARD_SERVICE) to obtain the object.
Before Android-11 (Android 3.0), the clipboard was used to pass data using the setText() and getText() methods, but after this version, these two methods were deprecated and instead used to pass ClipData objects instead. Compared with getText and setText, using ClipData objects to pass data is more in line with the object-oriented idea, and the data types that can be passed are also diversified.
Main steps:
Get the ClipboardManager object cm through getSystemService.
Use the() method to set the ClipData data object.
Get the ClipboardManager object cm in the new Activity.
Use the() method to get the ClipData data object of the clipboard, cd.
The passed data is obtained through (0).
Sample code
Save data:
protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); Button btn=(Button)findViewById(); (new () { @SuppressLint("NewApi") @Override public void onClick(View v) { //Get the clipboardClipboardManager cm=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); (("data", "Jack")); // Or write in 2 steps ClipData cd = ("label","Jack");(cd);Intent intent=new Intent(,); startActivity(intent); } }); }
Read data:
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub (savedInstanceState); setContentView(); ClipboardManager cm=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); ClipData cd=(); String msg=(0).getText().toString(); TextView tv=(TextView)findViewById(); (msg); }
The above method uses the String type data passed by the clipboard. If an object needs to be passed, the passed object must be serializable, and the serialization is marked by implementing the Serializable interface.
Main steps:
Create a class MyData that implements the Serializable interface.
Save data: Get the ClipboardManager, serialize the MyData object through the Base64 class, and then save it to the clipboard.
Extract data: In the new Activity, get the ClipboardManager, deserialize the serialized data, and use the Base64 class. The deserialized data is then processed.
Sample code:
Step 1:
public class MyData implements Serializable { private String name; private int age; public MyData(String name, int age) { super(); = name; = age; } public String getName() { return name; } public void setName(String name) { = name; } public int getAge() { return age; } public void setAge(int age) { = age; } }
Step 2:
protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); Button btn=(Button)findViewById(); (new () { @SuppressLint("NewApi") @Override public void onClick(View v) { //Get the clipboardClipboardManager cm=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); MyData mydata=new MyData("jack", 24); String baseToString=""; ByteArrayOutputStream bArr=new ByteArrayOutputStream(); try { ObjectOutputStream oos=new ObjectOutputStream(bArr); (mydata); baseToString=((), ); (); } catch (Exception e) { (); } (("data",baseToString)); Intent intent=new Intent(,); startActivity(intent); } }); }
Step 3:
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub (savedInstanceState); setContentView(); ClipboardManager cm=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); ClipData cd=(); String msg=(0).getText().toString(); byte[] base64_btye=(msg, ); ByteArrayInputStream bais=new ByteArrayInputStream(base64_btye); try { ObjectInputStream ois=new ObjectInputStream(bais); MyData mydata=(MyData)(); TextView tv=(TextView)findViewById(); (()); } catch (Exception e) { // TODO Auto-generated catch block (); } }
Summarize
To sum up, there are pros and cons to use clipboards to pass data. The clipboard is managed by the Android system, so the data stored in one place can be accessed by any application on this Android device. However, it is precisely because this device accesses the same clipboard, the data stored in the current program may be overwritten by other programs before use, resulting in the inability to ensure that the data is correctly obtained.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.