0

I have a basic Activity which mainly allows the user to change settings and save them. I also have a BroadcastReceiver which is launched on SMS_RECEIVED.

The main point of the app is to vibrate whenever a certain message is received until the user taps a button to make it stop. The activity is only there to allow the user to change settings and press the "Stop" button. In my onReceive method (BroadcastReceiver), I get the content of the last message received and make the phone vibrate if the message is equal to a certain string. All of that is working perfectly, the problem is when I want to make it stop. Right now, I'm trying to make a "Stop" button appear in the Activity when the phone starts vibrating.

I understand that UI elements should remain in the Activity and so what I'm trying to do is communicate between the Activity and my BroadcastReceiver. I've found here how to do that with an Observer. The problem though is that I want the app to function at any time, even at boot time. It's very easy with a BroadcastReceiver but since it requires the Activity to be shown to allow the user to stop the vibration, I have to start the activity if it isn't started already.

So what I do is this:

Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("SMSReceived", true);
context.startActivity(i);

ObservableObject.getInstance().updateValue(true);

The problem is, when there is no instance launched, it creates a new one and sends the extra boolean correctly but the updateValue method doesn't seem to get called at all (due to the previous instance I suppose?) and inversely, when there is an instance launched (in the background) the extra boolean doesn't get passed and the updateValue method gets called correctly.

I suppose I could just launch the Activity on boot and immediately put it in the background but it could cause problems if the user closes the application, at which point it would simply stop working until the user started it again since the Observer would have no instance to send data to.

Do you guys have any idea of what I could do to solve my problem? If it's not clear I can try to explain further.

Keagel
  • 21
  • 1
  • 6
  • Create a utility class and, for example, create in this class a public static boolean attribute, let's call it showStopButton with initial value false. When you receive the sms set the value of showStopButton to true. When the user open the activity check the value of showStopButton to decide if you have to show the button. – anemomylos Jul 22 '17 at 09:47
  • Thanks for the answer! How would I then stop the vibration since it's generated in the BroadcastReceiver? I need to "communicate" that the button has been pressed, basically. I could send a custom broadcast that I'd catch in my Broadcast class but then how would I be able to unregister a dynamically registered broadcast within a broadcast? I also just read that a BroadcastReceiver isn't made to execute code for more than 10 seconds and my vibration is supposed to last indefinitely - until the user presses "Stop", what else could I use? – Keagel Jul 22 '17 at 11:30
  • Success! I now have a BroadcastReceiver which checks all the necessary stuff then calls a service to vibrate, this service creates a notification and handles the stop button the way you said - with a static attribute. Works like a charm. Thanks! – Keagel Jul 22 '17 at 12:20

0 Answers0