【问题标题】:pause an activity meanwhile another one is running暂停一个活动,同时另一个活动正在运行
【发布时间】:2015-02-03 14:53:51
【问题描述】:

我想只使用一种方法来获得语音识别。

为了做到这一点,我创建了 3 个类

主类

public class Index extends Activity {

    private Button boton;
    private EditText texto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        boton = (Button)findViewById(R.id.boton);
        texto = (EditText) findViewById(R.id.texto);
        boton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                texto.setText(IVRecognition.getInstancia().getComando(Index.this));
            }
        });
    }
}

中间体

public class IVRecognition {

    //*******************singleton********************

    private static IVRecognition instancia;

    private IVRecognition (){}

    public static IVRecognition getInstancia(){
        if (instancia==null) instancia = new IVRecognition();
        return instancia;
    }

    //************************************************

    public static String resultado = null;

    public String getComando(Context content){
        Intent intent = new Intent(content, VRecognition.class);
        content.startActivity(intent);

        //pause here untill VRecognition.onActivityResult is executed

        return resultado;
    }
}

和识别一个

public class VRecognition extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startRecognition();
    }

    public void startRecognition (){
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,this.getPackageName());
        startActivityForResult(intent, 1 /*VOICE_RECOGNITION_REQUEST_CODE*/);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1 /*VOICE_RECOGNITION_REQUEST_CODE*/ && resultCode == RESULT_OK){
            ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            IVRecognition.getInstancia().resultado = result.get(0);
        }
        this.finish();
    }
}

问题是,当我使用content.startActivity(intent); 调用VRecognition 活动时,应用程序的执行会继续进行,因此在执行onActivityResult 之前,名为resultado 的变量具有空值,这会导致空返回价值。

希望你能帮助我。干杯

【问题讨论】:

  • 从不使 UI 线程休眠。为什么不在主活动中调用 startActivityFoResult?
  • 您好,seiterm,谢谢您的建议,我必须将它集成到另一个应用程序中,因此会被多次调用。我尝试更改 content.startActivity(intent); for ((Activity)content).startActivityForResult(intent,1);但没有成功。

标签: android multithreading sleep voice


【解决方案1】:

伊恩的回答很好。但根据您的评论,我建议使用 IntentService 和 BroadcastManager。这样你就不需要中间活动。您可以从任何需要 VR 结果(并实现 BroadcastReceiver)的活动中调用 startService(intent)。然后 IntentService 调用startActivityForResult(intent,1) 并广播结果。

更多信息: http://developer.android.com/training/run-background-service/index.html

【讨论】:

  • 嗨,seiterm,感谢您的时间和关注,我是 android 设备上的新手编程,我将调查 IntentService 和 BroadcastManager,我会及时通知您。
【解决方案2】:

听起来您想暂停执行,直到语音识别完成。您可能需要重新考虑这一点;您正在从您的 UI 线程调用 getComando(),因此您的应用程序 UI 将被冻结,直到识别完成。如果(很可能)识别时间超过 5 秒,系统将弹出“应用程序无响应”对话框。 (此外,由于您通过在进程中启动另一个活动来实现 getComando(),因此阻塞 getComando() 中的 UI 线程将阻止识别运行。)

正确的做法是使用完成回调。例如,您可以创建一个 IVRecognitionListener 接口:

public interface IVRecognitionListener { public void onRecognitionComplete(String resultado); }

并将其实例传递给getComando()。那么除了在onActivityResult()中设置IVRecognition.resultado,你还可以调用onRecognitionComplete()来通知调用者结果。

【讨论】:

  • 嗨,Ian,感谢您的建议,我绝对不会停止执行,我对您提出的解决方案很感兴趣,我会研究并及时通知您
  • seiterm 的回答更正确——您不应该在没有 UI 的情况下开始活动,这就是服务的用途。使用服务+广播的另一个优点是,您不会像使用我提出的解决方案那样持有对调用者的引用。如果调用者是一个活动,那么最终得到一个悬空引用将非常容易。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多