Android 显示Alertdialog并使用可控震源
我有一个实现RecognitionListener的类,如下所示:Android 显示Alertdialog并使用可控震源,android,android-alertdialog,android-vibration,Android,Android Alertdialog,Android Vibration,我有一个实现RecognitionListener的类,如下所示: public class listener implements RecognitionListener new AlertDialog.Builder(this) .setTitle("dd") .setMessage("aa") .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public class listener implements RecognitionListener
new AlertDialog.Builder(this)
.setTitle("dd")
.setMessage("aa")
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
我想显示alertdialog并使用可控震源,但这是不可能的,因为我需要提供我没有的上下文
我的alertdialog代码如下:
public class listener implements RecognitionListener
new AlertDialog.Builder(this)
.setTitle("dd")
.setMessage("aa")
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
但是AlertDialog.Builder(此)需要一个上下文,我的可控震源代码也有同样的问题:
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
getSystemService方法不可用
我的启动类的代码:
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
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,"voice.recognition.test");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
sr.startListening(intent);
解决这个问题的最好办法是什么 接受上下文对象的构造函数在这里工作得很好 声明上下文变量
private Context mContext;
然后声明一个接受上下文的构造函数
public listener(Context context){
super();
mContext = context
}
当您需要上下文时,请使用mContext而不是this
创建侦听器时,传递当前上下文
sr.setRecognitionListener(new listener(this));
另外,作为Java中的旁白,类名应该总是以大写字母开头,这样您的类应该是Listener而不是Listener,谢谢。super()是指implements RecognitionListener类吗?我调用该构造函数吗?super()不是必需的,因为编译器会为您放入它,但它确实指定了要调用的RecognitionListener构造函数,我想显式地声明它,提醒您正在调用它Hanks,这非常有帮助。