如何使用 startActivityForResult 两次启动相同的 activity

How to start the same activity using startActivityForResult twice

所以我正在尝试调用同一个 activity 两次,我知道会有更好的方法来做到这一点,但现在我只想要 2 个单独的数据记录。当我尝试 运行 这段代码时,舒张压首先被读取,这是无意的。有人可以解释为什么会这样吗?谢谢。

Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                //The following is required when ^^^ this is used
                i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Systolic Blood Pressure Value");
                startActivityForResult(i, SYSTOLIC_CHECK);
                //A different request code is required per activity called
                Intent j = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                j.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                j.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Diastolic Blood Pressure Value");
                startActivityForResult(j, DIASTOLIC_CHECK);

...

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
        String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH);
        String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);

        processPictureWhenReady(picturePath);
        // TODO: Show the thumbnail to the user while the full picture is being
        // processed.
    }
    else if ((requestCode == SYSTOLIC_CHECK) && resultCode == RESULT_OK)   {
        results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        System.out.println("Systolic BP: " + spokenText);
        //OutputStreamWriter out = new OutputStreamWriter(openFileOutput(STORETEXT, 0));
    }
    else if ((requestCode == DIASTOLIC_CHECK) && resultCode == RESULT_OK)   {
        results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        System.out.println("Diastolic BP: " + spokenText);
        //OutputStreamWriter out = new OutputStreamWriter(openFileOutput(STORETEXT, 0));
    }
    super.onActivityResult(requestCode, resultCode, data);
}

API 根本行不通 - 您无法像您尝试的那样对启动多个活动进行排队。

通常的做法是用 startActivityForResult 启动第一个 Activity,当第一个 activity returns 启动第二个时。

如果我没记错的话,因为你最后一次称你的舒张压 activity,所以它覆盖了你的收缩压 activity。 所以你 Diastolic Activity 将是用户第一个与之互动的人。

我只想指出@GreyBeardedGeek 说的是真的。 如果你只是开始收缩压 activity 可能会更好,然后,当你收到结果时,开始舒张压 Activity(在 OnActivityResult 中)。

这将防止您的活动未按您想要的顺序开始的问题。

编辑

你的代码看起来像这样

...else if ((requestCode == SYSTOLIC_CHECK) && resultCode == RESULT_OK)   {
    results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    String spokenText = results.get(0);
    System.out.println("Systolic BP: " + spokenText);
    //OutputStreamWriter out = new OutputStreamWriter(openFileOutput(STORETEXT, 0));

    //call second activity
    Intent j = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                j.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                j.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Diastolic Blood Pressure Value");
                startActivityForResult(j, DIASTOLIC_CHECK);
} ...

编辑

您的 activity 可能看起来像这样。

public class VoiceActivity extends ActionBarActivity {
    private String systole;
    private String diastole;

    private Button btnGetVoiceInput;

    private final int SYSTOLIC_CHECK=1, DIASTOLIC_CHECK=2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btnGetVoiceInput = (Button) findViewById(R.id.btnVoiceInput);

        btnGetVoiceInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                 //The following is required when ^^^ this is used
                 i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                 i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Systolic Blood Pressure Value");
                 startActivityForResult(i, SYSTOLIC_CHECK);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case SYSTOLIC_CHECK: {
                if(resultCode == RESULT_OK) {
                    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    systole = results.get(0);
                    System.out.println("Systolic BP: " + systole);

                    Intent j = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    j.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    j.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Read your Diastolic Blood Pressure Value");
                    startActivityForResult(j, DIASTOLIC_CHECK);
                }
                break;
            }
            case DIASTOLIC_CHECK: {
                if(resultCode == RESULT_OK) {
                    ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    diastole = results.get(0);
                    Log.v("VoiceInput", "Diastolic BP: " + diastole);

                    doSomething();
                }
                break;
            }
        }
    }

    private void doSomething() {
        //do what you want with both readings here
        String bloodpressure = systole + " / " + diastole;
        Log.v("Bloodpressure", bloodpressure);
    }
}

获得2个读数后,你可以用它们做点什么。 这确实仍然需要一些错误处理,但我还没有使用语音输入,所以我不知道它的值是什么 returns/can return.