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

时间:2015-02-24 15:10:10

标签: android start-activity

所以我试图实现两次调用相同的活动,我知道会有更好的方法来做到这一点但是现在我只想要2个单独的数据记录。当我尝试运行此代码时,首先读取舒张压BP,这是无意的。有人可以解释为什么会发生这种情况。谢谢。

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);
}

2 个答案:

答案 0 :(得分:2)

API根本不起作用 - 你不能像你想要的那样排队启动多个活动。

执行此操作的常规方法是使用startActivityForResult启动第一个Activity,并在第一个活动返回时启动第二个活动。

答案 1 :(得分:1)

如果我没有弄错,因为你将舒张活动称为最后一次,这就是你的收缩活动。 因此,舒张活动将是用户首先与之交互的活动。

我只想指出@GreyBeardedGeek所说的是真的。 如果你只是开始收缩活动可能会更好,然后,当你收到结果时,开始你的舒张活动(在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);
} ...

修改

您的活动可能如下所示。

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个读数后,你可以用它们做点什么。 这仍然需要一些错误处理,但我还没有使用语音输入,所以我不知道它返回/可以返回的值。