Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 在不同Activity之間傳遞數據的四種常用方法

在不同Activity之間傳遞數據的四種常用方法

編輯:關於Android編程

在Android中傳遞數據的方法非常多,本次介紹4中比較常用的數據傳遞方法:

1.通過Intent傳遞數據

2.通過靜態變量(static)傳遞數據

3.通過剪貼板(Clipboard)傳遞數據

4.通過全局對象傳遞數據

在TransmitDataActivity.java中

package mobile.android.transmit.data;
public class TransmitDataActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
Button button3=(Button)findViewById(R.id.button3);
Button button4=(Button)findViewById(R.id.button4);
button1.setOnClickListener(new ButtonListenner());
button2.setOnClickListener(new ButtonListenner());
button3.setOnClickListener(new ButtonListenner());
button4.setOnClickListener(new ButtonListenner());
}
class ButtonListenner implements OnClickListener{
@SuppressLint("NewApi")
@Override
public void onClick(View view) {
Intent intent=null;
switch (view.getId()) {
case R.id.button1:
intent=new Intent(TransmitDataActivity.this,MyActivity1.class);
//保存String類型的值
intent.putExtra("intent_string", "通過Intent傳遞的字符串");
//保存integer類型的值
intent.putExtra("intent_integer", 300);
Data data=new Data();
data.id=1000;
data.name="Android";
//保存可序列化的對象
intent.putExtra("intent_object", data);
//顯示用於接收數據的Activity
startActivity(intent);
break;
case R.id.button2:
intent=new Intent();
intent.setClass(TransmitDataActivity.this, MyActivity2.class);
//下面的代碼為MyActivity2中放入三個靜態變量賦值
MyActivity2.id=3000;
MyActivity2.name="保時捷";
MyActivity2.data=new Data();
MyActivity2.data.id=5555;
MyActivity2.name="Android";
startActivity(intent);
break;
case R.id.button3:
intent=new Intent(TransmitDataActivity.this,MyActivity3.class);
//獲取剪貼板對象(ClipboardManager)
ClipboardManager clipboard=(ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//創建Data對象
Data clipboardData=new Data();
//設置Data對象中字段的值
clipboardData.id=6666;
clipboardData.name="通過Clipboard傳遞的數據";
//創建字節數組輸出流對象,用於將Data對象轉換為字節流
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//用於保存Data對象生成Base64格式的字符串
String base64Str="";
try {
ObjectOutputStream oos=new ObjectOutputStream(baos);
//將Data對象寫入對象輸出流
oos.writeObject(clipboardData);
//將字節流進行Base64編碼
base64Str=Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
//獲取存儲文本數據的剪貼板數據對象(ClipData)
ClipData clipData=ClipData.newPlainText("data", base64Str);
//設置主剪貼板
clipboard.setPrimaryClip(clipData);
//顯示Myactivity3窗口
startActivity(intent);
break;
case R.id.button4:
//獲取MyApp對象
MyApp myApp=(MyApp) getApplicationContext();
myApp.country="美國";
myApp.data.id=1234;
myApp.data.name="飛碟";
intent=new Intent(TransmitDataActivity.this,MyActivity4.class);
startActivity(intent);
break;
default:
break;
}
}
}
}

Data類:這個類是可序列化的,也就是實現了java.io.Serializable接口的類。

package mobile.android.transmit.data;
import java.io.Serializable;
public class Data implements Serializable{
public int id;
public String name;
}

在Myactivity1.java中:package mobile.android.transmit.data;

public class MyActivity1 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
TextView textView=(TextView)findViewById(R.id.textview);
//獲得String類型的值
String intentString=getIntent().getStringExtra("intent_string");
//獲得Integer類型的值
int intentInteger=getIntent().getExtras().getInt("intent_integer");
//獲得Data類型的值
Data data=(Data) getIntent().getExtras().get("intent_object");
StringBuffer sb=new StringBuffer();
sb.append("Intent_string:");
sb.append(intentString);
sb.append("\n");
sb.append("intent_integer:");
sb.append(intentInteger);
sb.append("\n");
sb.append("data.id:");
sb.append(data.id);
sb.append("\n");
sb.append("data.name:");
sb.append(data.name);
sb.append("\n");
//在屏幕上輸出傳遞過來的值
textView.setText(sb.toString());
}
}

在Myactivity2.java中:

package mobile.android.transmit.data;
public class MyActivity2 extends Activity{
public static String name;
public static int id;
public static Data data;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
TextView textView=(TextView)findViewById(R.id.textview);
StringBuffer sb=new StringBuffer();
sb.append("name:");
sb.append(name);
sb.append("\n");
sb.append("id:");
sb.append(id);
sb.append("\n");
sb.append("data.id:");
sb.append(data.id);
sb.append("\n");
sb.append("data.name:");
sb.append(data.name);
sb.append("\n");
textView.setText(sb.toString());
}
}

在Myactivity3中:

package mobile.android.transmit.data;
public class MyActivity3 extends Activity{
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
TextView textView=(TextView)findViewById(R.id.textview);
//獲取剪貼板對象
ClipboardManager clipboardManager=(ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//從剪貼板中獲取Base64編碼字符串
String base64Str=clipboardManager.getPrimaryClip().getItemAt(0).getText().toString();
//將Base64編碼字符串編碼成字節數組
byte[] buffer=Base64.decode(base64Str, Base64.DEFAULT);
ByteArrayInputStream bais=new ByteArrayInputStream(buffer);
try {
ObjectInputStream ois=new ObjectInputStream(bais);
//將字節流還原成Data對象
Data data=(Data) ois.readObject();
//將Base64編碼原文和Data對象字段值顯示在TextView控件中
textView.setText(base64Str+"\n\ndata.id:"+data.id+"\ndata.name:"+data.name);
} catch (Exception e) {
e.printStackTrace();
} 
}
}

在Myactivity4.java中:

package mobile.android.transmit.data;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MyActivity4 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
TextView textView=(TextView) findViewById(R.id.textview);
MyApp myApp=(MyApp) getApplicationContext();
textView.setText("MyApp.country:"+myApp.country+"\nMyApp.data.id:"+myApp.data.id+"\nMyApp.data.name:"+myApp.data.name);
}
}

關於在不同Activity之間傳遞數據的四種常用方法小編就給大家介紹這麼多,希望對大家有所幫助!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved