Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method in activity from a service

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?

like image 777
UpCat Avatar asked Feb 15 '13 14:02

UpCat


People also ask

How do I call a method of service from activity?

You can call startService(intent) and bindService(mIntent, mConnection, BIND_AUTO_CREATE) in any order. Binding and Starting a service are two independent things.

How do I send a callback from service to activity?

You need to create a BroadcastReceiver in your activity (be sure to register it in onResume() and unregister it in onPause() ) and notify it via a broadcast, providing an Intent .

Can you start an activity from a service?

You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.


2 Answers

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

like image 57
Tony Wickham Avatar answered Oct 03 '22 02:10

Tony Wickham


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 ;)

like image 40
fweigl Avatar answered Oct 03 '22 03:10

fweigl