1

I created a WebService class that handles multiple different requests to a web service, this class Broadcast's and intent with a different key depending on what method was originally called in the WebService class. I'm stuck on how to properly handle this on the WebServiceReceiver...

Here's the important part from the WebService:

   //Broadcast the intent with data received from service call.
    broadcastIntent.putExtra(broadcastIntentKeyName, response.toString());
    sendBroadcast(broadcastIntent);

Here's my onReceive:

public class WebServiceReceiver extends BroadcastReceiver
{

    public WebServiceReceiver() {}

    @Override
    public void onReceive(Context context, Intent intent) {

        Debug.waitForDebugger();

        //NOTE: Not sure if i'm approaching this the right way, sure doesn't seem like it... 
        //Its possible that some of these will be NULL.
        String GetRequestForRouteWithDriverId_DATA = intent.getStringExtra("Helper_GetRequestForRouteWithDriverId");
        String StoreDataInServer_DATA = intent.getStringExtra("Helper_StoreDataInServer");
        String SubmitDriverRouteData_DATA = intent.getStringExtra("Helper_SubmitDriverRouteData");

        MainActivity.getInstance().updatetextViewControl(GetRequestForRouteWithDriverId_DATA);

    }
}

What I have does work, But like I mentioned in my code comment, it doesn't feel like its the proper way.

Is there a better way to approach this? I simply want to re-use this onReceive to handle all WebService Broadcasts.

Dayan
  • 7,634
  • 11
  • 49
  • 76
  • Why not send a single string along with an integer/enum and then `switch()` on the integer? – alzee Dec 18 '15 at 20:22
  • @user3137702 I can't switch, I need to get the data from the intent first and i have no way of knowing which is the `intent` thats not `NULL` without attempting to read from each one as i'm doing now - At least i'm not aware of any way to do this, which is why i'm asking the question. – Dayan Dec 18 '15 at 20:37
  • If you have no control over the intent being sent, then what you're doing is the right basic approach, bad as it smells. – alzee Dec 18 '15 at 20:40
  • @user3137702 I updated question to show the `broadcastIntent` found in my `WebService` class, I'm simply doing a `putExtra`. – Dayan Dec 18 '15 at 20:46
  • Well you could put a switch or map lookup or something there, before you send the broadcast. It would at least put the smelly part adjacent to the web call that is necessitating it, and allow you to send different intent types to your own receiver rather than shoehoring everything into the extras. – alzee Dec 18 '15 at 21:49

1 Answers1

2

Maybe you can use Intent.setAction() method

    broadcastIntent.setAction("Your action");
    broadcastIntent.putExtra("Your Extra");
    sendBroadcast(broadcastIntent);

Then on your receiver...

public class WebServiceReceiver extends BroadcastReceiver
{

public WebServiceReceiver() {}

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if(action.equals("Your Action")){
            String yourExtra = intent.getStringExtra("Your extra")
        }

    }
}
Dayan
  • 7,634
  • 11
  • 49
  • 76
Melvin Mauricio
  • 387
  • 1
  • 6