Java – the IntentReceiver component is not allowed to bind to a service
This is my code:
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.speech.tts.TextToSpeech; import android.telephony.SmsMessage; import android.util.Log; public class SmsBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context,Intent intent) { Object[] rawMsgs = (Object[])intent.getExtras().get("pdus"); for (Object raw : rawMsgs) { SmsMessage message = SmsMessage.createFromPdu((byte[])raw); Log.v("[SMS]:",message.getMessageBody()); TextToSpeech tts = new TextToSpeech(context,null); } } }
An error occurred while I tried to initialize texttospeech Obviously, I can't bind to the service in broadcastreceiver Is there any solution?
Solution
You have a bigger problem than that
First, onreceive() is called from the previous priority on the main application thread You can't spend more than a few milliseconds there without causing problems with the foreground application, such as stuttering in the game
Second, when an SMS message comes in, you don't know what the user is doing They may be playing games They may be in the middle of the call You can't, can't, can't unilaterally decide that you will play a person's SMS through text to voice You're easy to sue, maybe worse
I urge you to stop coding now and sit down and think - really - what you're trying to do and the consequences of your approach