vivek
vivek

Reputation: 13

How to Call Method of one activity from service in Android?

I have one activity 'A' which extends mapActivity which calls service 'B', and Service 'B' which extends service. I'am getting Latitude and Longitude in service 'B'. Now i need to call the method in Activity 'A' and pass the Latitude and Longitude contineosly as soon as the location is updated. In that method I have code to display the Location. Thanks in advance..

Upvotes: 1

Views: 247

Answers (2)

waqaslam
waqaslam

Reputation: 68167

You cant get reference of your Activity from Service unless you set a STATIC reference to it - which is highly not recommended and is considered to be a culprit for memory leak issues.

Instead, I would suggest you to make use of android's broadcasting mechanism using context.sendBroadcast and BroadcastReceiver.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006539

How to Call Method of one activity from service in Android?

You don't.

Either:

  • This should not be a Service in the first place (with all the logic within the activity), or
  • Use LocalBroadcastManager to send a message from the service to the activity, or
  • Use a Messenger to send a message from the service to the activity, or
  • Use a PendingIntent created by createPendingResult() on the activity to send a message from the service to the activity, or
  • Use a ResultReceiver, or the Otto library, or a variety of other means to send a message from the service to the activity

Upvotes: 2

Related Questions