Android uses a clipboard to transfer data

  • 2021-11-30 01:33:54
  • OfStack

There are also a few techniques that can be used to transfer data between Activity. Both windows and Linux operating systems will support a technology called clipboard, that is, one program copies one data to the clipboard, and then any other program can obtain data from the clipboard. This technology also exists in Android system.

Using the clipboard will use the ClipboardManager object, which is used against the clipboard, and ClipboardManager is used to manipulate the clipboard, but there is no constructor (singleton mode) for public, which needs to be obtained using Activity. getSystemService (Context. CLIPBOARD_SERVICE).

Prior to the Android-11 (Android 3.0) release, the setText () and getText () methods were used to pass data using clipboards, but after this release, these two methods were discarded and replaced by passing ClipData objects. Compared with getText and setText, using ClipData objects to transfer data is more in line with the idea of object-oriented, and the data types that can be transferred are diversified.  

Main steps:

Get the ClipboardManager object cm through getSystemService.

Set the ClipData data object using the cm. setPrimaryClip () method.

Gets the ClipboardManager object cm in the new Activity.

Use the cm. getPrimaryClip () method to get the ClipData data object of the clipboard, cd.

The data passed in is obtained through cd. getItemAt (0).

Sample code

Save data:


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 Shear Plate  
ClipboardManager cm=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); 
cm.setPrimaryClip(ClipData.newPlainText("data", "Jack")); //  Or points 2 Step writing  ClipData cd = ClipData.newPlain("label","Jack");cm.setPrimaryClip(cd);
Intent intent=new Intent(MainActivity.this,otherActivity.class); 
startActivity(intent); 
} 
}); 
} 

Read data:


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); 
}

In the above way, the data of String type is passed by clipboard. If 1 object needs to be passed, the passed object must be serializable, and serialization is marked by implementing 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 store it in the clipboard.
Fetch data: In the new Activity, get ClipboardManager and deserialize the serialized data, also using Base64 class. Then the deserialized data is processed.

Sample code:

Step 1:


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 2:


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 Shear Plate  
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 3:


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, There are advantages and disadvantages to using clipboard to transfer data, The clipboard is managed by 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 that the data stored in the current program may be overwritten by other programs before use, resulting in no guarantee of correct data acquisition.


Related articles: