【发布时间】: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