使用LocationListener进行服务

时间:2014-04-22 06:56:21

标签: android gps android-service locationlistener

我的代码是

    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分钟内调用,也不会在Locationchanged调用时调用。

1 个答案:

答案 0 :(得分:0)

您必须先获取更新,然后才能获得更新:

    LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

另外,请记住在清单中添加ACCESS_FINE_LOCATION作为权限