在拨打电话时语音识别对话框

时间:2012-07-03 17:32:00

标签: android

我有BroadcastReceiver类接收来电。一旦我的手机接到来电,我想发射Voice Recognizer。没有任何接口可以调用RecognizerIntent。应该在手机响铃时自动调用Just RecognizerIntent。这可能吗?

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

Sravan

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

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

        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

在致电FLAG_ACTIVITY_NEW_TASK之前,您可能需要使用i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);向您的意图添加startActivity()标记。这是最小的,你可能想要一些其他标志。有关列表,请参阅this page