第六集android使用剪切板传递数据

MainActivity.java

 1 package com.fxp.intent3;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.ObjectOutputStream;
 5 
 6 import android.os.Bundle;
 7 import android.annotation.SuppressLint;
 8 import android.app.Activity;
 9 import android.content.ClipboardManager;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.util.Base64;
13 import android.view.Menu;
14 import android.view.View;
15 import android.widget.Button;
16 import android.widget.TextView;
17 
18 @SuppressLint("NewApi")
19 public class MainActivity extends Activity {
20     private Button goButton;
21     private TextView hiTextView;
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.main);
26         goButton= (Button)this.findViewById(R.id.goButton);
27         goButton.setOnClickListener(new View.OnClickListener() {
28             
29             @Override
30             public void onClick(View v) {
31 /*                ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
32                 String name = "jack";
33                 clipboardManager.setText(name);
34                 Intent intent = new Intent(MainActivity.this,OtherActivity.class);
35                 startActivity(intent);*/
36                 MyData myData = new MyData("jack", 23);
37                 //将对象转换成字符串
38                 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
39                 String base64String = "";
40                 try {
41                     ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
42                     objectOutputStream.writeObject(myData);
43                     base64String = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
44                     objectOutputStream.close();
45                 } catch (Exception e) {
46                     // TODO: handle exception
47                 }
48                 ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
49                 clipboardManager.setText(base64String);
50                 Intent intent = new Intent(MainActivity.this,OtherActivity.class);
51                 startActivity(intent);
52             }
53         });
54     }
55 
56     @Override
57     public boolean onCreateOptionsMenu(Menu menu) {
58         // Inflate the menu; this adds items to the action bar if it is present.
59         getMenuInflater().inflate(R.menu.main, menu);
60         return true;
61     }
62 
63 }
View Code

 

MyData.java

 1 package com.fxp.intent3;
 2 
 3 import java.io.Serializable;
 4 
 5 public class MyData implements Serializable {
 6     private String name;
 7     private int age;
 8     public String getName() {
 9         return name;
10     }
11     public void setName(String name) {
12         this.name = name;
13     }
14     public int getAge() {
15         return age;
16     }
17     public void setAge(int age) {
18         this.age = age;
19     }
20     public MyData(String name, int age) {
21         super();
22         this.name = name;
23         this.age = age;
24     }
25     @Override
26     public String toString() {
27         return "MyData [name=" + name + ", age=" + age + "]";
28     }
29     
30 }
View Code

 

OtherActivity.java

 1 package com.fxp.intent3;
 2 
 3 import java.io.ByteArrayInputStream;
 4 import java.io.ObjectInputStream;
 5 
 6 import android.os.Bundle;
 7 import android.annotation.SuppressLint;
 8 import android.app.Activity;
 9 import android.content.ClipboardManager;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.util.Base64;
13 import android.view.Menu;
14 import android.view.View;
15 import android.widget.Button;
16 import android.widget.TextView;
17 
18 public class OtherActivity extends Activity {
19     private Button backButton;
20     private TextView msg;
21     @SuppressLint("NewApi")
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.other);
26         msg = (TextView)this.findViewById(R.id.msg);
27         backButton= (Button)this.findViewById(R.id.backButton);
28         ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
29         String msgString = clipboardManager.getText().toString();
30         //msg.setText(msgString);
31         byte[] base64_byte=Base64.decode(msgString, Base64.DEFAULT);
32         ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64_byte);
33         try {
34             ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
35             MyData myData = (MyData)objectInputStream.readObject();
36             msg.setText(myData.toString());
37         } catch (Exception e) {
38             e.printStackTrace();
39         }
40     }
41 
42     @Override
43     public boolean onCreateOptionsMenu(Menu menu) {
44         // Inflate the menu; this adds items to the action bar if it is present.
45         getMenuInflater().inflate(R.menu.main, menu);
46         return true;
47     }
48 
49 }
View Code

 

main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <TextView
 9         android:id="@+id/hiTextView"
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:text="@string/hello_world" />
13 
14     <Button
15         android:id="@+id/goButton"
16         android:layout_width="fill_parent"
17         android:layout_height="wrap_content"
18         android:layout_marginTop="20dp"
19         android:text="使用剪切板传递数据" />
20 
21 </LinearLayout>
View Code

 

other.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <TextView
 9         android:id="@+id/msg"
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:text="第二页" />
13 
14     <Button
15         android:id="@+id/backButton"
16         android:layout_width="fill_parent"
17         android:layout_height="wrap_content"
18         android:layout_marginTop="20dp"
19         android:text="使用剪切板传递数据" />
20 
21 </LinearLayout>
View Code

 

AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.fxp.intent3"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:maxSdkVersion="16" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.fxp.intent3.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <activity android:name="com.fxp.intent3.OtherActivity" android:label="@string/app_name" ></activity>
26     </application>
27 
28 </manifest>
View Code

 

posted @ 2013-10-23 18:38  当年Java小强  阅读(255)  评论(0编辑  收藏  举报