나는 클릭 할 때 기본 음성 인식기 활동을 열 수있는 위젯을 가지고 있습니다. 이제이 작업의 데이터를 일부 서비스를 수행하는 내 서비스로 보내 주길 바랍니다. 다음 코드Android 음성 인식기의 데이터를 서비스에 전달하는 방법은 무엇입니까?
@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");
}
}