IntentReceiver components not allowed to contact services

Here 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); } } } 

I get an error when trying to initialize TextToSpeech . Apparently, I can't get attached to services when in BroadcastReceiver . Is there a workaround for this?

+4
source share
2 answers

Is there any workaround for this?

You have a lot more problems.

First, onReceive() is called in the main thread of the application, at the foreground priority. You cannot spend more than a few milliseconds without creating problems for the foreground application, for example, making you stutter in the game.

Secondly, you do not know what the user does when sending an SMS message. They can play the game. They may be in the middle of a phone call on their speakerphone. You cannot, you cannot, CANNOT decide unilaterally that you are going to communicate with a person through a text message. You can easily file a lawsuit and possibly worse.

I urge you to stop coding now, and lean back and think - really think - about what you are trying to do and the consequences of your approach.

0
source

You can check the following link

+1
source

Source: https://habr.com/ru/post/1339491/


All Articles