使用Intent和Bundle传递数据。首先,在发送方活动中,设置一个Intent,并使用Bundle将数据附在上面。然后,使用putExtras()方法将Bundle附加到Intent中。最后,调用startActivity()方法将Intent发送到接收方活动。在接收方活动中,与发送方活动相同,在onCreate()方法中解析Intent,然后从Bundle中提取数据。
实现代码示例:
// 发送方活动中的代码
String data = "这是要传递的数据。" ;
Intent intent = new Intent("com.example.receiver.ACTION" ); // 接收方活动的action Bundle bundle = new Bundle(); bundle.putString( "data_key" , data); intent.putExtras(bundle); startActivity(intent);
// 接收方活动中的代码
Intent intent = getIntent(); String action = intent.getAction(); if ( "com.example.receiver.ACTION" .equals(action)) { // 发送方活动的action Bundle bundle = intent.getExtras(); String data = bundle.getString( "data_key" ); // 然后使用数据做一些操作 }