一、简单的Intent传值:
MainActivity.java里:
Intent intent=new Intent(MainActivity.this,AnotherActivity.class);
intent.putExtra("name","Arong");
intent.putExtra("age", 22);
intent.putExtra("Adress","N8-402");
startActivity(intent);
AnotherActivity.java:
Intent intent=getIntent();
String name=intent.getStringExtra("name");
int age=intent.getIntExtra("age", 0);
String adress=intent.getStringExtra("Adress");
二、使用全局变量传递:
使用这个方法需要先在AndroidManifest.xml注明全局变量,就是application中name属性要注明.MyApp,当然你要根据自己的文件名来注明。
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android_intent5.MainActivity"
android:label="@string/app_name" >
MyApp.java文件:
package com.example.android_intent5;
import android.app.Application;
public class MyApp extends Application {
private String name;
private int 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 void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
}
MainActivity.java
package com.example.android_intent5;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
private MyApp myApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myApp=(MyApp)getApplication();
myApp.setName("Arong");
myApp.setAge(2);
Intent intent=new Intent(MainActivity.this,AnotherActivity.class);
startActivity(intent);
}
});
}
}
AnotherActivity:
package com.example.android_intent5;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AnotherActivity extends Activity {
private TextView textView;
private MyApp myApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
textView = (TextView) this.findViewById(R.id.msg);
myApp=(MyApp)getApplication();
textView.setText("myApp.Name:"+myApp.getName()+"\n myApp.age:"+myApp.getAge());
}
}
三、 可序列化的传递
过程:将MyData对象转换为字符串写入clipboardmanager
再在anotheractivity里获取重新转化成对象 显示在TextView上
MyData.java:
package com.example.android_intent6;
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 + "]";
}
}
注意:这里的MyData实现了Serializable接口,这个接口可以实现将对象转化成字节流的形式,详细可查API;
MainActivity.java:
package com.example.android_intent6;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MyData myData=new MyData("Arong", 22);
//将对象先转化成字符串
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
//构造一个新的ByteArrayOutputStream读取默认大小为32字节。 如果超过32字节被写入这个实例中,底层字节数组将扩大。
String bas64String="";
try {
//Constructs a new ObjectOutputStream that writes to the OutputStream output.
ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
//Writes an object to objectOutputStream
objectOutputStream.writeObject(myData);
//Base64-encode the given data and return a newly allocated String with the result.
bas64String=Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
objectOutputStream.close();
} catch (Exception e) {
// TODO: handle exception
}
ClipboardManager clipboardManager=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText(bas64String);//将字符串装入剪贴板,之后再AnotherActivity里读取出来
Intent intent =new Intent(MainActivity.this,AnotherActivity.class);
startActivity(intent);
}
});
}
}
AnotherActivity.java:
package com.example.android_intent6;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Base64;
import android.widget.TextView;
public class AnotherActivity extends Activity {
private TextView textView;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
textView = (TextView) findViewById(R.id.msg);
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
@SuppressWarnings("deprecation")
String msgsString = clipboardManager.getText().toString();
//Decode the Base64-encoded data in input and return the data in a new byte array.
byte[] base64_byte = Base64.decode(msgsString, 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
}
}
}