0

次のように RecognitionListener を実装するクラスがあります。

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(this) にはコンテキストが必要です。これは、私のバイブレーター コードと同じ問題です。

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);

これを解決する最良の方法は何ですか?

4

1 に答える 1

1

ここでは、コンテキスト オブジェクトを受け取るコンストラクタがうまく機能します。

コンテキスト変数を宣言する

private Context mContext;

次に、コンテキストを取るコンストラクターを宣言します

public listener(Context context){
    super();
    mContext = context
}

コンテキストが必要な場合は、代わりに mContext を使用しますthis

リスナーを作成するときに、現在のコンテキストを渡します

sr.setRecognitionListener(new listener(this));

また、Javaクラス名は常に大文字で始まる必要があるため、クラスはリスナーではなくリスナーにする必要があります

于 2013-10-22T09:51:08.370 に答える