android activity之间通过剪切板传递数据

  • 内容
  • 评论
  • 相关

MainActivity

package com.example.clipboard;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
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.view.View.OnClickListener;
import android.widget.Button;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button button01 = (Button) findViewById(R.id.button1);
		button01.setOnClickListener(this);
	}

	@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;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.button1:
			intent = new Intent(this,MainActivity01.class);
			ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
			
			Data clipboardData = new Data();
			clipboardData.id = 123456;
			clipboardData.name = "hellogxp";
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			String base64Str = "";
			try{
				ObjectOutputStream oos = new ObjectOutputStream(baos);
				oos.writeObject(clipboardData);
				base64Str = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
				oos.close();
			}catch(Exception e){
				
			}
			
			ClipData clipdata = ClipData.newPlainText("data", base64Str);
			clipboard.setPrimaryClip(clipdata);
			startActivity(intent);
			break;

		default:
			break;
		}
		
	}

}

MainActivity01

package com.example.clipboard;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.Base64;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity01 extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_activity01);
		
		TextView textView01 = (TextView) findViewById(R.id.textview01);
		ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
		String base64Str = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
		byte[] buffer = Base64.decode(base64Str, Base64.DEFAULT);
		ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
		try{
			ObjectInputStream ois = new ObjectInputStream(bais);
			Data data = (Data) ois.readObject();
			textView01.setText(base64Str + "\n\ndata.id" + data.id + "\ndata.name" + data.name);
		}catch(Exception e){
			
		}
	}

	@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_activity01, menu);
		return true;
	}

}

完整项目文件
http://pan.baidu.com/s/1jGwUlP8

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注