gpt4 book ai didi

java - startActivityForResult 两次

转载 作者:行者123 更新时间:2023-12-01 14:53:06 29 4
gpt4 key购买 nike

我有两个按钮和音频 ListView ,对于第一个按钮,我使用 LongClick 和 startActivityForResult 方法从 MediaPlayer 的另一个 Activity 获取字符串。但我也想使用第二个按钮。我需要使用 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();
}

};

最佳答案

所以基本上您需要了解请求代码,这是您传递给 startActivityForResult 的第二个参数。从第一个按钮调用时可以使用 0 作为请求代码,从第二个按钮调用时可以使用 1。

在第一次单击按钮时启动结果 Activity

 startActivityForResult(i, 0);

单击第二个按钮时启动 Activity 以获取结果

 startActivityForResult(i, 1);

并且您需要在 onActivityResult(..) 方法中添加检查,以了解哪个 Activity 返回了 Intent 结果,如下所示。

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
if(requestCode == 0) {
// handling code of startActivityForResult of first button
Bundle basket = data.getExtras();
s = basket.getString("arnswer");
}
if(requestCode == 1) {
// handling code of startActivityForResult of second button
Bundle basket = data.getExtras();
s1 = basket.getString("arnswer");
}

}
}

编辑:-不要使用 0 和 1 作为请求代码,而是定义常量以便将来可读。

关于java - startActivityForResult 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14632253/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com