码迷,mamicode.com
首页 > 移动开发 > 详细

Android 使用剪切板传递数据

时间:2015-01-16 10:01:05      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

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

首先来个简单点吧。

首先在,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 = 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 使用剪切板传递数据

标签:

原文地址:http://www.cnblogs.com/zhujiabin/p/4227741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!