1、在Activity之间传递数据还可以利用一些技巧,无论是Windows还是Linux操作系统,都支持一种叫做剪切板的技术(某一程序将数据复制到剪切板上,其它的任何程序都可以从剪切板中获取数据);
2、新建一个名为“android_intent3”的Android工程;
3、在main.xml文件中添加Button:
4、新建布局文件”other.xml“,添加”TextView“,代码:
5、声明一个Activity,”OtherActivity“,并继承Activity,在此类中创建”onCreate“方法;
6、回到“Main.java”中,添加成员Button,并向Android系统调用剪切板,构造意图并启动,代码:
package com.android.myintent;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 从Android系统中调用剪切板的服务
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
String name = "Jack";
clipboardManager.setText(name);// 向剪切板添加数据
Intent intent = new Intent(Main.this, OtherActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
9、点击“OtherActivity.java”,添加获取剪切板方法,获取剪切板数据,设置到“textView”上,代码:
package com.android.myintent;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView = (TextView) this.findViewById(R.id.msg);
ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
String msgString = clipboardManager.getText().toString();//从剪切板中获取数据
textView.setText(msgString);
}
public OtherActivity() {
// TODO Auto-generated constructor stub
}
}
10、在“AndroidManifest.xml”上添加“Activity”:
11、运行:
12、点击按钮,居然提示无法运行,后来经过查找,发现是因为在Android11版本之后,原来利用剪切板传递数据使用的setText()和getText()方法被弃用了,只能使用ClipData对象来代替了~下面我们来对上面的程序做出一点改动即可~
第8步中橙色部分的程序替换为:
ClipData clipData = ClipData.newPlainText("Label", "Jack");
clipboardManager.setPrimaryClip(clipData);
第9步中橙色部分的程序替换为:
ClipData clipData = clipboardManager.getPrimaryClip();//从剪切板中获取数据
Item item = clipData.getItemAt(0);
textView.setText(item.getText().toString());
13、如果我们想在剪切板中传递复杂数据,比如一个对象:先建一个类“MyData”,并使其实现“serializable”接口(在Superclass选项里):
14、在新建类中添加成员“name”、“age”并提供构造方法,代码如下:
import java.io.Serializable;
public class MyData implements Serializable {
private String name;
private int age;
@Override
public String toString() {
return "MyData [name=" + name + ", age=" + 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;
}
}
15、进入“Main.java”,将原来添加的代码注释掉,然后实例化一个MyData对象,代码如下(用到了Base64工具类):
package com.android.myintent;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipData.Item;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.util.Base64;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 从Android系统中调用剪切板的服务
// ClipboardManager clipboardManager = (ClipboardManager)
// getSystemService(Context.CLIPBOARD_SERVICE);
// 向剪切板添加数据
// ClipData clipData = ClipData.newPlainText("label", "Jack");
// clipboardManager.setPrimaryClip(clipData);
// Intent intent = new Intent(Main.this, OtherActivity.class);
// startActivity(intent);
MyData myData = new MyData("Jack", 23);
// 将对象转换为字符串
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String base64String = "";
try {// 捕获异常
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
byteArrayOutputStream);
objectOutputStream.writeObject(myData);
base64String = Base64.encodeToString(
byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
objectOutputStream.close();
} catch (Exception e) {
// TODO: handle exception
}
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData
.newPlainText("label", base64String);
clipboardManager.setPrimaryClip(clipData);
Intent intent = new Intent(Main.this, OtherActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
16、进入“OtherActivity.java”,修改代码如下:
package com.android.myintent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.ClipData.Item;
import android.os.Bundle;
import android.util.Base64;
import android.widget.TextView;
public class OtherActivity extends Activity {
private TextView textView;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView = (TextView) this.findViewById(R.id.msg);
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = clipboardManager.getPrimaryClip();// 从剪切板中获取数据
Item item = clipData.getItemAt(0);
String msgString = item.getText().toString();
byte[] base64_byte = Base64.decode(msgString, Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
base64_byte);
try {
ObjectInputStream objectInputStream = new ObjectInputStream(
byteArrayInputStream);
MyData myData = (MyData) objectInputStream.readObject();
textView.setText(myData.toString());
} catch (Exception e) {
// TODO: handle exception
}
}
public OtherActivity() {
// TODO Auto-generated constructor stub
}
}
Ps:未完待续~