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

没关系,我想通了。您将不得不放置一个额外的虚拟活动,从小部件中触发它并从那里调用语音识别器。您始终可以使用 onActivityResult() 取回结果。

于 2013-05-27T14:12:45.673 回答
0

你不应该需要一个虚拟活动。这可能是与 PendingIntent 的请求代码相关的问题。有关可能与您的问题相关的错误报告,请参阅https://code.google.com/p/android/issues/detail?id=63666

您是否尝试使用其他请求代码?

于 2014-10-23T19:37:46.413 回答