1

クリックするとデフォルトの音声認識アクティビティを開くウィジェットがあります。ここで、このアクティビティからのデータを、何らかの処理を行うサービスに送信したいと考えています。次のコードを使用して音声認識アクティビティを開始できます

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    ComponentName thisWidget = new ComponentName(context,
            MicWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.mic_widget_layout);

         // this intent points to activity that should handle results
         Intent serviceIntent = new Intent(context,
         SpeechWidgetService.class);
         // this intent wraps results activity intent
         PendingIntent resultsPendingIntent = PendingIntent.getService(
         context, REQUEST_CODE, serviceIntent, 0);

        // this intent calls the speech recognition
        Intent voiceIntent = new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Jarvis");
        voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT,
                resultsPendingIntent);

        // this intent wraps voice recognition intent
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                REQUEST_CODE, voiceIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.arc_widget, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    Log.d(TAG, "clicking..");
}

ウィジェットをクリックすると、すべてが正常に機能し、話すことができ、話すのをやめるとウィジェットが閉じます。問題は、サービスがまったく呼び出されていないことです。私は何を間違っていますか?今のところ、データが入ってくるかどうかをチェックするためのダミー サービスがあります。

public class SpeechWidgetService extends Service {

private static final String TAG = "SpeechWidgetService";

@Override
public IBinder onBind(Intent intent) {
    ArrayList<String> voiceResults = intent.getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS); 
    Log.d(TAG, voiceResults.get(0));
    Toast.makeText(getBaseContext(), voiceResults.get(0), Toast.LENGTH_SHORT).show();
    return null;
}

@Override
public void onCreate() {
    Log.d(TAG, "onCreate");
    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
    Log.d(TAG, "onStart");
}

}

4

2 に答える 2

0

ダミーのアクティビティは必要ありません。これは、PendingIntent のリクエスト コードに関連する問題である可能性があります。問題に関連する可能性のあるバグ レポートについては、https://code.google.com/p/android/issues/detail?id=63666を参照してください。

別のリクエストコードを使用しようとしましたか?

于 2014-10-23T19:37:46.413 に答える
0

気にしないで、私はそれを理解しました。追加のダミー アクティビティを配置し、ウィジェットから起動して、そこから音声認識エンジンを呼び出す必要があります。onActivityResult() を使用して、いつでも結果を取得できます。

于 2013-05-27T14:12:45.673 に答える