我有两个按钮和音频列表视图,对于第一个按钮,我使用 LongClick 和 startActivityForResult 方法从 MediaPlayer 的另一个活动中获取字符串。但我也想使用第二个按钮。我需要使用 startActivityForResult 为另一个 MediaPlayer 获取另一个字符串,这是我的代码:
public class MainActivity extends Activity {
String setData, gotArnswer,s,s1;
MediaPlayer mMediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaPlayer = new MediaPlayer();
Button b1 =(Button) findViewById(R.id.button1);
Button b2 =(Button) findViewById(R.id.button2);
b2.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
return false;
}
});
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(s); //using string here
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
});
b1.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Activityone.class);
startActivityForResult(i, 0);
return false;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Bundle basket = data.getExtras();
s = basket.getString("arnswer");
}
}
我的 ListView onClick()
:
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
Intent person = new Intent();
Bundle backpack = new Bundle();
backpack.putString("arnswer", filename);
person.putExtras(backpack);
setResult(RESULT_OK, person);
finish();
}
};