startActivityForResult两次

时间:2013-01-31 18:04:56

标签: java android android-intent

我有两个按钮和音频列表视图,第一个按钮我使用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();
}

};

2 个答案:

答案 0 :(得分:1)

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

点击第一个按钮

的结果开始活动
 startActivityForResult(i, 0);

点击第二个按钮

的结果的开始活动
 startActivityForResult(i, 1);

你需要在onActivityResult(..)方法中添加一个检查来知道哪个活动会返回意图结果,如下所示。

    @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作为请求代码,定义常量,以便将来可读。

答案 1 :(得分:0)

您的部分代码无用

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;

        }
    });

使用两个听众 One Long Click Listener b1.setOnLongClickListener(new View.OnLongClickListener(){})

和其他像这样 b1.setOnClickListener(new OnClickListener(){})

然后,您将能够通过一个按钮完成两项工作