Which broadcast receiver to use?

I read this answer here , as well as this one here , and I'm trying to figure out what is right for my case.

I start the service inside onCreate() , where I make HTTP requests, and I get id as the response. Then I translate this id and get it in my activity.

The problem is that the user can explicitly go to another action by simply opening the mailbox and selecting an option, and I can skip the broadcast.

Obviously, all my actions extend an abstract class that extends Activity , as mentioned here , but I'm not 100% sure that this is the best solution. What if the user decides to close the application before I get the identifier?

Edit : application architecture

  • The user captures the image using intent to open the camera application and get the path to the image file.
  • FormActivity begins when the user can add some image information.
  • The user uploads the files, and I transfer the data that the user just entered into QuizActivity .
  • In onCreate() of QuizActivity run Service , where I:
    • create an empty entry on the server and I get id as the response and
    • Upload image to server
  • This is id I get a response from the server, then transmit it.
  • QuizActivity has a registered entryIdReceiver , where it receives an id , and saves it in a closed field until the user decides to either leave the action or click to load the quiz (if, of course, he entered the data for the quiz)
  • If the user clicks on download, I start IntentService using id and quiz data.
  • If the user opens the box and selects another Activity or presses to create a quiz, I run IntentService to load the id with the "empty quiz data" and transfer the user to MainActivity .

The problem is that if uer closes the application during QuizActivity , and I have not yet received an id , or the user decides to switch to another Activity using the box without adding a quiz. I still need to start the service and load the id with the "empty quiz data".

+5
source share
2 answers

This is pretty good using an abstract class where you handle all the actions and just send a callback to your activity. Using this example in your previous question seems to me like an EventBus.

And it's even better to use a special class and interfaces instead of an abstract class, because you can use FragmentActivity, AppCombatActivity, etc.

For example, you have your own class, which receives the result from your service and sends all the actions registered to it. Calling the result from network requests with interfaces:

 public class NetRequestReceiver extends BroadcastReceiver { private List<Activities> registeredActivities; public static void getInstance () { //Continue singleton initialing! //.... } @Override public void onReceive(Context context, Intent intent) { for (Activity act : registeredActivities) { if (act instanceOf ReceivingCallback) { act.onReceiveMessage(intent); } else throw Exception("Activity missing ReceivingCallback"); } } public void registerActivity (Activity, activity) { if (!registeredActivities.contains(activity)) { registeredActivities.add(activity); } } public void unRegisterActivity (Activity, activity) { if (registeredActivities.contains(activity)) { registeredActivities.remove(activity); } } public interface ReceivingCallback { onReceiveMessage (Intent intent); } } 

Then in all your actions add the next listener. But (!) Do not forget the registrar above in your service to get the result!

 public class MainActivity extends Activity implements NetRequestReceiver.ReceivingCallback { public void onStop () { super.onStop() NetRequestReceiver.getInstance().unRegisterActivity(this); } public void onResume () { super.onResume() NetRequestReceiver.getInstance().registerActivity(this); } @Override public onReceiveMessage (Intent intent) { //put here whaterver you want your activity //to do with the intent received } } 

What do you think using the design above? Now we have one receiver and callback as an interface. Thus, you can use Fragment, Activity, FragmentActivity and another class to get the result from the service via Broadcast and (!) Without copying, pasting the same behavior!

It also looks good, because we share different layers - presentation, view and receiver. You are invoking a network request in the Service. This service sends the result to Broadcast, and then sends the data to all registered activities.

Yes, this is similar to EventBus, but based on your question, this is just what you need to listen for the connection from the service in different actions and with a better structure.

+2
source

Maybe you can send a sticky broadcast. The system will deter it even from destroyed activities, and you can immediately get the intention when your receiver registry property.

But the sendStickyBroadcast notification is outdated and doesn't forget to announce

 android.permission.BROADCAST_STICKY 

in AndroidManifest.xml if you decide to use it.

+1
source

All Articles