首先看一下Intent的官方的API。
但是我们并没有发现直接传递Object类型的方法,但是在开发中经常会需要传递一个Object类型或者ArrayList《Object》类型的数据,那么我们应该怎样实现呢?
方法如下:
下面我们将student对象从Activity传递给Service。
Student类
package com.example.intenttest;
public class Student implements Serializable{
private String name;
private String sex;
private int id;
public Student(String name, String sex, int id) {
super();
this.name = name;
this.sex = sex;
this.id = id;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", id=" + id + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Activity类:
package com.example.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建Student对象
Student student = new Student("小灰灰", "男", 1);
// 通过Intent传递对象给Service
Intent intent = new Intent(MainActivity.this, MyService.class);
intent.setAction("action");
intent.putExtra("student", student);
startService(intent);
}
}
Service类:
package com.example.intenttest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if("action".equals(intent.getAction())){
Student student = (Student) intent.getSerializableExtra("student");
Log.i("tag", "学生对象的toString():"+student);
}
return super.onStartCommand(intent, flags, startId);
}
}
将ArrayList《Object》对象从MainActivity传递给SecondActivity:
MainActivity类:
package com.example.intenttest;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 这里必须为ArrayList,不能为List
ArrayList<Student> list = new ArrayList<Student>();
// 创建Student对象
Student student1 = new Student("小灰灰", "男", 1);
Student student2 = new Student("大灰狼", "男", 2);
Student student3 = new Student("红太狼", "女", 3);
list.add(student1);
list.add(student2);
list.add(student3);
// 通过Intent传递对象给Service
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.setAction("action");
intent.putExtra("studentlist", list);
startActivity(intent);
}
}
(注意:MainActivity类中在声明list的时候必须用ArrayList而不能用List,否则就会出现如图所示的提示错误信息,
)
SecondActivity类:
package com.example.intenttest;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if ("action".equals(intent.getAction())) {
ArrayList<Student> list = (ArrayList<Student>) intent.getSerializableExtra("studentlist");
for(int i = 0;i<list.size();i++){
Log.i("tag", "student:" + list.get(i));
}
}
}
}
以上是使用putExtra(String name, Serializable value)方法来进行Object和ArrayList《Object》的传递的代码。
通过 putExtra(String name, Parcelable value)和putParcelableArrayListExtra(String name, ArrayList《? extends Parcelable》 value)两个方法与putExtra(String name, Serializable value)的使用方法相同,只不过是序列化的方式不同。