首页 > 代码库 > Android 使用剪切板传递数据

Android 使用剪切板传递数据

使用剪切板传递数据,可以传递简单的数据,也可以传递可序列化的对象。

首先来个简单点吧。

首先在,mainActivity.xml文件中加入一个button按钮

private Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // TODO Auto-generated method stub                button = (Button)this.findViewById(R.id.button);                button.setOnClickListener(new View.OnClickListener() {                                        @SuppressLint("NewApi")                    @Override                    public void onClick(View v) {                        // TODO Auto-generated method stub                                                //  第一种情况:剪切板传递常用类型数据//                        ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);//从android系统中调用剪切板服务//                        String name = "jack";//                        clipboardManager.setText(name);//                        Intent intent = new Intent(MainActivity.this,OtherActivity.class);//                        startActivity(intent);                                                //  第二种情况:剪切板传递可序列化对象                        MyData mydata = http://www.mamicode.com/new MyData("jack","beijing");                        //把对象转化成字符串                        ByteArrayOutputStream byteArrayOutPutStream = new ByteArrayOutputStream();                        String baseString = "";                        try {                            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutPutStream);                            objectOutputStream.writeObject(mydata);                            baseString = Base64.encodeToString(byteArrayOutPutStream.toByteArray(), Base64.DEFAULT);                            objectOutputStream.close();                        } catch (Exception e) {                            // TODO: handle exception                        }                        ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);                        clipboardManager.setText(baseString);                        Intent intent = new Intent(MainActivity.this,OtherActivity.class);                        startActivity(intent);                    }                });            }

在此之前当然需要新建一个对象了,新建MyData.java

package com.example.android_intent3;import java.io.Serializable;public class MyData implements Serializable {    private String age;    private String name;    public MyData(String age, String name) {        super();        this.age = age;        this.name = name;    }    @Override    public String toString() {        return "MyData [age=" + age + ", name=" + name + "]";    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public MyData() {        // TODO Auto-generated constructor stub    }}

当然也需要在mainActivity.xml文件中加入一个button

 <Button        android:id="@+id/button"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="使用剪切板传递数据" />

还需要在新建一个布局文件other.xml文件加入:

<TextView android:id="@+id/msg"  android:layout_width="fill_parent"    android:layout_height="fill_parent"></TextView>

对了,还得新建OtherActivity.java文件,加入:

private TextView textView;    public OtherActivity() {        // TODO Auto-generated constructor stub    }    @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);//        String msgString = clipboardManager.getText().toString();        //textView.setText(msgString);        //第二种情况        textView = (TextView)this.findViewById(R.id.msg);        //获取Android剪切板服务        ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);        //获取剪切板里的数据        String msgString = clipboardManager.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        }    }

最后,当然不能忘了,在清单文件中加入OtherActivity.java.

 <activity android:name=".OtherActivity"></activity>

 

Android 使用剪切板传递数据