package com.example.serilizaldemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private TextView title; private TextView url; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); title = (TextView)this.findViewById(R.id.title); url = (TextView)this.findViewById(R.id.url); button = (Button)this.findViewById(R.id.button1); final Game game = new Game(); game.setTitle(title.getText()+""); game.setUrl(url.getText()+""); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,SecondActiviity.class); // 1 封装 序列化 Bundle bundle = new Bundle(); bundle.putSerializable("love", game); intent.putExtra("game", bundle); startActivity(intent); } }); } @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; } }
package com.example.serilizaldemo; import java.io.Serializable; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class SecondActiviity extends Activity { private static final String Tag= "SecondActiviity"; public SecondActiviity() { // TODO Auto-generated constructor stub } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); Intent intent = getIntent(); Bundle bundle = intent.getBundleExtra("game"); // 2 反序列化! 取出数据。 Serializable obj = bundle.getSerializable("love"); if(obj != null && obj instanceof Game) { Game g = (Game)obj; Log.i(Tag, "--url="+g.getUrl()); Log.i(Tag, "--title="+g.getTitle()); } Log.i(Tag, "--title="); } }
package com.example.serilizaldemo; import java.io.Serializable; import java.util.List; // 实现 可序列化 实现 可续列话 接口 public class Game implements Serializable{ // 定义自己的序号 private final static long serialVersionUID = 1234567890L; private String title; private String url; private long fileSize; // private List<Go> list; public Game() { // TODO Auto-generated constructor stub } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public long getFileSize() { return fileSize; } public void setFileSize(long fileSize) { this.fileSize = fileSize; } }
原文地址:http://blog.csdn.net/love_javc_you/article/details/39496637