在IntentService中获取地理位置的上下文信息

4
我正在创建一个Android类库,该类库调用内部REST API,并利用Android的Geocoder类(特别是getFromLocation()),该类需要Context。我计划将该库制作成IntentService,以便异步运行,但是我无法解决如何处理Context的问题(GeoLoc调用在库的另一个类中,该类是IntentService调用的一部分)。
我的问题是,如何在IntentService内部获取实例化android.location.Geocoder所需的上下文?
2个回答

10

每个Service都是一个Context。您可以使用this或者getApplicationContext()


我假设我将在Intent服务中调用它并将其传递给我的对象? - mjsalinger
是的!在你的IntentService中调用它。 - Flávio Faria
当你想在onHandleIntent中使用SystemService时,不能在'this'上下文中调用getSystemService,而应该始终使用'getApplicationContext()'来访问getSystemService()。 - Nishant Soni

0

IntentService继承Service,而Service继承Context。

在IntentService内部,您可以自由地使用"this"作为Context。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接