Android入门篇四:使用剪切板在Activity之间传递对象数据

使用剪切板在Activity之间传递对象数据,要用到一个特殊的类---android.util.Base64.java,将对象转换成字符串,再在剪切板中放入要传递的数据,在接收端将字符串还原成对象,在把数据取出来,通过例子来看一下如何实现:

工程结构图:


运行结果如下:

MainActivity截图:


OtherActivity截图:

Android入门篇四:使用剪切板在Activity之间传递对象数据_第1张图片

开始上源代码:

MainActivity.java:

package com.intent.activity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button)findViewById(R.id.btOpenOtherActivity);
        btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Person person = new Person("wulianghuan","22");
				//将对象转换成字符串
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				String base64String = "";
				
				try {
					ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
					objectOutputStream.writeObject(person);
					base64String = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
					objectOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				//从Android系统中调用剪切板的服务
				ClipboardManager clipboardManager = (ClipboardManager)
				getSystemService(Context.CLIPBOARD_SERVICE);
				//在剪切板中放入要传递的数据
				clipboardManager.setText(base64String);
				//定义一个意图
				Intent intent = new Intent(MainActivity.this,OtherActivity.class);
				startActivity(intent);
			}
		});
    }
}

OtherActivity.java:

package com.intent.activity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class OtherActivity extends Activity {
	private TextView text_name;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		text_name = (TextView) findViewById(R.id.name);
		//从Android系统中调用剪切板的服务
		ClipboardManager clipboardManager = (ClipboardManager)
		getSystemService(Context.CLIPBOARD_SERVICE);
		String getString = clipboardManager.getText().toString();
		//字符串还原成对象
		byte[] base64_byte = Base64.decode(getString, Base64.DEFAULT);
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64_byte);
		try {
			ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
			Person person = (Person)objectInputStream.readObject();
			Log.i("OtherActivity", person.toString());
			//设置文本框的数据
			text_name.setText(person.toString());
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
}

由于main.xml文件比较简单,这里就不给出源代码了

注意在AndroidManifest.xml中对OtherActivity进行声明。

你可能感兴趣的:(android,android,android,Activity,Activity,传递对象)