標籤:
首先看一下Intent的官方的API。
傳遞一些基本類型資料的方法如下:
- putExtra(String name, int value)
- putExtra(String name, String value)
- putExtra(String name, float value)
- putExtra(String name, double value)
- putExtra(String name, long value)
- putExtra(String name, boolean value)
- putExtra(String name, byte value)
- putExtra(String name, char value)
傳遞基本類型ArrayList對象的方法如下:
- putIntegerArrayListExtra(String name, ArrayList《Integer》 value)
- putStringArrayListExtra(String name, ArrayList《Integer》 value)
但是我們並沒有發現直接傳遞Object類型的方法,但是在開發中經常會需要傳遞一個Object類型或者ArrayList《Object》類型的資料,那麼我們應該怎樣實現呢?
方法如下:
傳遞Object類型:
- putExtra(String name, Serializable value)
- putExtra(String name, Parcelable value)
傳遞ArrayList《Object》類型:
- putExtra(String name, Serializable value)
- putExtra(String name, Parcelable value)
- putParcelableArrayListExtra(String name, ArrayList《? extends Parcelable》 value)
下面我們將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)的使用方法相同,只不過是序列化的方式不同。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Android中Intent傳遞Object和ArrayList<Object>對象---筆記