简体   繁体   中英

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..

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM