【问题标题】:Voice recognize dialog while ringing phone打电话时语音识别对话框
【发布时间】:2012-07-03 17:32:00
【问题描述】:

我有接收来电的 BroadcastReceiver 类。一旦我的手机接到来电,我想启动语音识别器。不会有任何接口可以调用 RecognizerIntent。 Just RecognizerIntent 应该在电话响铃时自动调用。这可能吗?

如果有人分享此代码,我将不胜感激。

斯拉万

这就是我想要做的。但它没有弹出语音识别器。

package srv.phone.calls;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;


public class ServiceReceiver extends BroadcastReceiver {
MediaPlayer mediaPlay;
Toast toast;
private int REQUEST_CODE = 1234;

@Override
public void onReceive(Context context, Intent intent) {

    Bundle extras = intent.getExtras();
    if (extras != null) {
        String state = extras.getString(TelephonyManager.EXTRA_STATE);
        Log.w("DEBUG", state);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.w("DEBUG", phoneNumber);
              // mediaPlay = MediaPlayer.create(context, R.raw.sweet);
              // mediaPlay.start();
               toast = Toast.makeText(context, "Call from " + phoneNumber, Toast.LENGTH_LONG);
               toast.show();

               Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
               i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
               i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
               context.startActivity(i); //Not working

        }
    }
}

}

【问题讨论】:

  • 你试过什么?什么有效,什么无效?你有什么想要分享的代码吗?
  • 在该特定行的上下文中定义“不工作”。您是否收到任何错误消息?另外,在这种情况下,我会使用Log.d() 而不是Log.w()。为不好的事情保存 WARN。

标签: android


【解决方案1】:

在调用startActivity() 之前,您可能需要使用i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);FLAG_ACTIVITY_NEW_TASK 标志添加到您的意图中。这是最低要求,您可能需要一些其他标志。列表见this page

【讨论】:

  • 感谢您的帮助。现在它显示带有未知错误的语音识别器。如何从活动结果中捕获此错误。
  • 读取日志。它说什么?
  • 现在,我可以从 BroadcastReceiver 调用语音识别活动,如我的代码所示,它工作正常。但是如何在 BroadcastReceiver 类中获得此语音识别活动的结果(例如:onActivityResult)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多