1

我有接收来电的 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

        }
    }
}

}

4

1 回答 1

0

您可能需要在调用之前FLAG_ACTIVITY_NEW_TASK使用将标志添加到您的意图中。这是最低要求,您可能需要一些其他标志。请参阅此页面以获取列表。i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity()

于 2012-07-03T23:21:09.860 回答