Reputation: 45
I'm trying to implement a Broadcast Receiver who should receive Intent from a Service which is running in the background, however it is not working.
In my main Activity I just bind the service and start it via onClick
on a Button.
In my SpeechRecognizer
Service class I create a new BroadcastReceiver
(see code below).
public void onCreate() {
Log.d(TAG, "onCreate");
if (SpeechRecognizer.isRecognitionAvailable(this)){ //check if a SpeechRecognizer is available
this.sr = SpeechRecognizer.createSpeechRecognizer(this);
this.sr.setRecognitionListener(new listener());
commandsReceiver = new CommandsReceiver();
ConnectivityManager cm =(ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
testfilter = new IntentFilter();
registerReceiver(commandsReceiver,testfilter);
} else {
Toast.makeText(this,"Please install a SpeechRecognizer on your system.", Toast.LENGTH_LONG).show(); //alert if speech-recognizer is not installed on the device
Log.d(TAG, "no SpeechRecognizer available");
this.onDestroy();
}
}
In my onResult
I do it like this:
Intent intent = new Intent();
intent.setAction(CommandsReceiver.TEST);
sendBroadcast(intent);
In my CommandsReceiver
I just got a simple String
and a Log message:
public class CommandsReceiver extends BroadcastReceiver {
public static final String TEST = "de.speech.TEST_INTENT";
@Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadCastReceiver", "Intent received"+intent);
}
}
However I'm not getting the Log.d()
message.
I hope you can help me out.
Upvotes: 2
Views: 2268
Reputation: 67296
Seems you are creating an IntentFilter without any Action as
testfilter = new IntentFilter();
instead of,
testfilter = new IntentFilter(CommandsReceiver.TEST);
so, register your BroadCast using,
testfilter = new IntentFilter(CommandsReceiver.TEST);
registerReceiver(commandsReceiver,testfilter);
Upvotes: 2