How to call a method in activity from a service

34,203

Solution 1

I would register a BroadcastReceiver in the Activity and send an Intent to it from the service. See this tutorial: http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html It might look a bit long but you'll want to learn how to use those anyway ;)

Solution 2

Assuming your Service and Activity are in the same package (i.e. the same app), you can use LocalBroadcastManager as follows:

In your Service:

// Send an Intent with an action named "my-event". 
private void sendMessage() {
  Intent intent = new Intent("my-event");
  // add data
  intent.putExtra("message", "data");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

In your Activity:

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

  // Register mMessageReceiver to receive messages.
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("my-event"));
}

// handler for received Intents for the "my-event" event 
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // Extract data included in the Intent
    String message = intent.getStringExtra("message");
    Log.d("receiver", "Got message: " + message);
  }
};

@Override
protected void onPause() {
  // Unregister since the activity is not visible
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onPause();
}

From section 7.3 of @Ascorbin's link: http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html#ownreceiver_localbroadcastmanager

Solution 3

There are many different ways to achive this. One of them to use Handler and Messanger classes. The idea of the method is to pass Handler object from Activity to Service. Every time Service wants to call some method of the Activity it just sends a Message and Activity handles it somehow.

Activity:

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                showToast(msg.what);
            }
        };

        final Intent intent = new Intent(this, MyService.class);
        final Messenger messenger = new Messenger(handler);

        intent.putExtra("messenger", messenger);
        startService(intent);
    }

    private void showToast(int messageId) {
        Toast.makeText(this, "Message  " + messageId, Toast.LENGTH_SHORT).show();
    }
}

Service:

public class MyService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            final Messenger messenger = (Messenger) intent.getParcelableExtra("messenger");
            final Message message = Message.obtain(null, 1234);

            try {
                messenger.send(message);
            } catch (RemoteException exception) {
                exception.printStackTrace();
            }
        }

        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
Share:
34,203
DarkLeafyGreen
Author by

DarkLeafyGreen

Tackling Complexity in the Heart of Software

Updated on April 30, 2020

Comments

  • DarkLeafyGreen
    DarkLeafyGreen about 4 years

    There is a service that listens for some voice. If voice matches a string a certain method is invoked in the service object.

    public class SpeechActivationService extends Service {
    
         public static Intent makeStartServiceIntent(Context pContext){    
    
             return new Intent(pContext, SpeechActivationService.class);
         }
    
         //...
    
         public void onMatch(){
             Log.d(TAG, "voice matches word");
         }
    
         //...
    }
    

    This is how I start the service in my activity:

    Intent i = SpeechActivationService.makeStartServiceIntent(this);
    startService(i);
    

    From this service method, how can I invoke a method that resides in the activity object? I don't want access from activity to service, but from service to activity. I already read about handlers and broadcasters but could not find/understand any example. Any ideas?

  • DarkLeafyGreen
    DarkLeafyGreen over 11 years
    My service extends from service and not IntentService
  • Vladimir Mironov
    Vladimir Mironov over 11 years
    You can use Service as well. This code just shows the idea. I updated my answer and now it uses Service instead of IntentService
  • DarkLeafyGreen
    DarkLeafyGreen over 11 years
    +1 Thank you, I already managed it with broadcast receiver inchoo.net/mobile-development/android-development/…
  • Ewoks
    Ewoks over 10 years
    is there any performance difference in this two approach (messanger vs broadcasting) in case we are sending commands/values from service to activity often, let's say 20 times per second? thanks
  • Nickmccomb
    Nickmccomb almost 8 years
    Thanks Tony! Great answer
  • Alexandre Prazeres
    Alexandre Prazeres almost 7 years
    Simple and direct answer. Thank you.
  • Dnyaneshwar
    Dnyaneshwar over 3 years
    I'm using the same in my project. It was working fine. Now I'm getting null exception in service class while LocalBroadcastManager.getInstance(getApplicationContext()).s‌​endBroadcast(intent)‌​; java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.BroadcastReceiver.onReceive(android.content.‌​Context, android.content.Intent)' on a null object reference