Obtaining Context for Geolocation within an Intent

2019-05-07 10:12发布

问题:

I am creating an Android Class library that makes calls to an internal REST API and also utilizes Android's Geocoder class (specifically getFromLocation()) which requires Context. I was planning on making the library an IntentService to allow for it to be run asynchronously, but I can't figure out how to handle Context (the GeoLoc call is in a separate class that is part of the library that the IntentService calls).

My question is, how do I obtain context necessary to instantiate android.location.Geocoder from within an IntentService?

回答1:

Every Service is a Context. You can use this or getApplicationContext().



回答2:

IntentService Inherits Service, and Service Inherits Context.

You are free to use "this" as Context whenever you needs it inside your IntentService.