Android study notes sample code for passing values in Activity using a clipboard

  • 2020-05-10 18:50:47
  • OfStack

There are other techniques that can be used to transfer data between Activity. Either the windows or Linux operating systems support a technique called a clipboard, where a program copies some data onto the clipboard, and then any other program can get the data from the clipboard, as in Android.

The ClipboardManager object is used with the clipboard, and this pair of clipboards is used with the ClipboardManager image to manipulate the clipboard, but there is no constructor for public (singleton mode), so you need to use Activity.getSystemService (Context.CLIPBOARD_SERVICE) to get the object.

Before the release of Android-11 (Android 3.0), the setText() and getText() methods were used to transfer data using a clipboard, but after this release, both methods were abandoned and ClipData objects were passed instead. Compared with getText and setText, the use of ClipData objects to transfer data is more object-oriented, and the types of data 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.
Get the ClipboardManager object cm in the new Activity.
Get the ClipData data object of the clipboard, cd, using the cm.getPrimaryClip () method.
Get the data passed in through cd.getItemAt (0).

The 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) { 
// Obtain shear plate  
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:
 
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 a clipboard to pass data of type String. If one object needs to be passed, the passed object must be serializable, which is marked by implementing the Serializable interface.
Main steps:
Create a class MyData that implements the Serializable interface.
Save data: get ClipboardManager, serialize MyData objects through Base64 class, and then store them in the clipboard.
Fetch data: in the new Activity, get ClipboardManager, deserialize the serialized data, again using 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(); 
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) { 
// Obtain 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(); 
} 
} 

conclusion
Transfer data to sum up, using the clipboard, clipboard for Android system management, so in 1 place in the data, on the Android equipment to any application can access, but precisely because of this device access are all with a clipboard, may lead to the current procedure in the data, before use is covered by other programs, lead to cannot be guaranteed to get the data correctly.

Related articles: