1

I am aware of the BroadCastReceiver, but how can I use it to call a method in my activity. So if I get a notification from my service, a button in my UI turns red, and red being the object that has been sent from the service and turning red the method that has been called by the activity. sorry for bad english :)

thablee
  • 23
  • 3
  • Do I need a Thread in my Activity and receive the object of the BroadCastReceiver or can I just start a methode designed to start with the object? pls help – thablee Jul 20 '17 at 17:23
  • Use an event bus. See https://stackoverflow.com/a/45146379/115145. – CommonsWare Jul 20 '17 at 17:24
  • Thanks for the response, but I think that is not what i am looking for. – thablee Jul 20 '17 at 17:32
  • Then perhaps you should explain, in computer programming terms, why an event bus is not what you are looking for. – CommonsWare Jul 20 '17 at 17:39
  • So I have to implement the event bus in im service, because my service communicates with for instance a server. – thablee Jul 20 '17 at 17:54
  • You would post messages to the event bus from the service, providing information about state changes. Your UI would register for messages on the bus when it is in the foreground, and it could then react to those messages and update the UI to match. – CommonsWare Jul 20 '17 at 18:02

2 Answers2

1

Register a BroadcastReceiver in your activity

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String message = intent.getStringExtra("message");
        Log.d("receiver", "Got message: " + message);

        if (message.equals("eventOne"))
        {
            //do something
        }
        else if (message.equals("eventTwo"))
        {
            //do something else
        }
    }
};

Override onResume and onDestroy of your activity

@Override
protected void onResume()
{
    super.onResume();

    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter("my-event"));
}

@Override
protected void onDestroy()
{
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);

    super.onDestroy();
}

To check if your service is running add the following on your activity

private static boolean isServiceRunning(String serviceName, Context context)
{
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
    {
        if (serviceName.equals(service.service.getClassName()))
        {
            return true;
        }
    }
    return false;
}

Call it like this

boolean isServiceRunning = isServiceRunning(MyService.class.getName(), this.getApplicationContext());

if (!isServiceRunning)
{
    Intent startMyServiceIntent = new Intent(this.getApplicationContext(), MyService.class);

    startService(startMyServiceIntent);
}

Finally on your service add a method like that and call it whenever you want

private void sendMessage(String event)
{
    Intent intent = new Intent("my-event");

    intent.putExtra("message", event);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    sendMessage("eventOne");

    return START_STICKY;
}

And don't forget to add your service to manifest

<application
    ...
    ...
    <service android:name=".MyService"/>
</application>
nKalai
  • 132
  • 1
  • 2
  • 5
  • I tested it out and it would not send the string. – thablee Jul 20 '17 at 18:29
  • I've tested it now and it's working. I guess your service isn't running. Check my answer again. I've edited it so you can check your service and start it. – nKalai Jul 20 '17 at 19:34
0

You can use a BroadcastReceiver, an event bus or an observable. In your activity, listen for the event and call a method that updates your UI how you want.

I do something similarly for GCM intent service. I use an rxBus that posts when the service is triggered. My activity subscribes to the bus and reacts when it sees the post.

BHogan
  • 73
  • 7
  • Ok, but can I pass an Object with a BoradcastReceiver? – thablee Jul 20 '17 at 18:10
  • Yes, you pass an intent and fill it in with your object. Check out google's example https://github.com/googlesamples/google-services/blob/master/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart/RegistrationIntentService.java#L76-L77 – BHogan Jul 20 '17 at 18:14