Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of broadcast Receiver to use?

Tags:

android

I have read this answer here and also this here and I'm trying to figure out what fits best for my case.

I start a Service inside onCreate() where I make an HTTP requests and I get an id as a response. I then broadcast this id and receive it in my activity.

The problem is that the user can obviously navigate to another activity just by opening the drawer and selecting an option and I can miss the broadcast.

I can obviously have all my Activities extend an abstract class which extends Activity like is mentioned here but I'm not 100% sure its the best solution. What if user decides to quit the app before I receive the id ?

Edit : app architecture

  • User captures an image using an Intent to open camera app and get the path of the image file.
  • FormActivity starts where user can add some details about the image.
  • User clicks upload and I pass the data user has just entered to QuizActivity.
  • In onCreate() of QuizActivity I start an Service where I :
    • create an empty entry to server and I get an id as a response and
    • upload image to server
  • That id I get as a response from server I then broadcast it.
  • QuizActivity has an entryIdReceiver registered where receives the id and stores it in a private field until user decides to either leave the activity or click to upload the quiz ( if he entered data for the quiz of course )
  • If user clicks upload I start an IntentService with the id and the quiz data.
  • If User opens drawer and select another Activity or clicks cancel to create a quiz I start an IntentService to upload the id with the "empty quiz data" and move user to MainActivity.

The problem is : what if uer closes app while on QuizActivity and I haven't yet receive the id, or user decides to got to another Activity using the drawer without adding a quiz. I still have to start a service and upload the id with "empty quiz data".

like image 318
Mes Avatar asked Oct 18 '22 05:10

Mes


2 Answers

It's pretty good, by using abstract class, where you handle all action, and just send callback to your activity. Using that example in your question above seems to me like EventBus.

And even better using special class and interfaces, instead of abstract class, because you may want use FragmentActivity, AppCombatActivity, etc.

For example, your have your own class, which receiving result from your service and send all registered to him activities. Invoking result from net 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 you activities add next listener. But (!) don't forget register receiver above in you Service for receiving 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, we get, using design above? We now have single Receiver and Callback as an interface. So you can use Fragment, Activity, FragmentActivity, and other class, for receiving result from Service via Broadcast and (!) without copy pasting same behavior!

Also it's looks nice, because we split of different layer - presentation, view, and receiver. You call net request in Service. This Service send result to Broadcast, and then he sends data to all registered activities.

Yes, it's seems like EventBus, but based on your question it's just what you need for listen connection from service to different activities, and with better structure.

like image 197
GensaGames Avatar answered Nov 15 '22 05:11

GensaGames


Maybe you can send a sticky broadcast. The system will keep it even the activity destroyed and you can receive the intent immediately when your register property receiver.

But notice sendStickyBroadcast is deprecated and don't forget declare

android.permission.BROADCAST_STICKY

in your AndroidManifest.xml if you decide to using it.

like image 29
Huang Wei Avatar answered Nov 15 '22 05:11

Huang Wei