首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LocationListener的服务

使用LocationListener的服务
EN

Stack Overflow用户
提问于 2014-04-22 14:56:21
回答 1查看 458关注 0票数 0

我的代码是

代码语言:javascript
复制
    public class AlarmService extends Service implements LocationListener {
        public int onStartCommand(Intent intent,int flags,int startId){
        getLocation();
        if(reachedLocation()){
        Intent alarmIntent=new Intent(getApplicationContext(),MainActivity.class);
        startActivity(alarmIntent);
        stopSelf();
        }
@Override
        public void onLocationChanged(Location location) {
        getLocation();
        if(reachedLocation()){
        Intent alarmIntent=new Intent(getApplicationContext(),MainActivity.class);
        startActivity(alarmIntent);
        stopSelf();
        }
}

永远不会调用onLocationChanged。我将两次更新之间的最短时间设置为1分钟。它不会在1分钟内调用,也不会在位置更改时调用。

EN

回答 1

Stack Overflow用户

发布于 2014-04-22 15:17:34

您必须先请求更新,然后才能通过以下方式获取更新:

代码语言:javascript
复制
    LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

此外,请记住将ACCESS_FINE_LOCATION作为权限添加到清单中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23212575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档