当前位置: 代码迷 >> Android >> Android 数据传递-经过剪切板传递数据
  详细解决方案

Android 数据传递-经过剪切板传递数据

热度:63   发布时间:2016-04-28 03:48:07.0
Android 数据传递-通过剪切板传递数据
?在Activity之间数据传递还可以利用一些技巧,不管是Windows还是Linux操作系统,都会支持一种叫剪切板的技术,也就是某一个程序将一些数据复制到剪切板上,然后其他的任何程序都可以从剪切板中获取数据。
?

?

?案例一

?

package com.android.myintent;import java.io.ByteArrayOutputStream;import java.io.ObjectOutputStream;import java.net.ContentHandler;import android.R.color;import android.R.integer;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.text.ClipboardManager;import android.util.Base64;import android.view.View;import android.widget.Button;public class Main extends Activity {	/** Called when the activity is first created. */	private Button button;	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.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);				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);				clipboardManager.setText(base64String);				Intent intent =new Intent(Main.this,OtherActivity.class);				startActivity(intent);			}		});	}}

?

? ?

package com.android.myintent;import java.io.ByteArrayInputStream;import java.io.ObjectInputStream;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.text.ClipboardManager;import android.util.Base64;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();		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		}	}}

? ?

package com.android.myintent;import java.io.Serializable;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;	}	@Override	public String toString() {		return "MyData [name=" + name + ", age=" + age + "]";	}}

?

  相关解决方案