如何在睡眠期间接收位置更改

时间:2014-12-21 12:28:51

标签: android

我正在编写一个请求位置更新的应用:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval, 0, locationListener);
当设备开启但是当进入睡眠时,

onLocationChanged 会被调用,它会停止接收进一步的更新。我跟着this线程,但正如我所说 - 它不起作用。

侦听器在前台服务中注册,需要一直工作。我希望只有在收到新位置时才从睡眠状态唤醒电池。

编辑:当然,当我在onCreate()方法中获取wakelock并在onDestroy()方法中释放它以进行服务时,一切都有效但我不想这样做。

1 个答案:

答案 0 :(得分:0)

如果您想使用后台任务,请使用此服务

public class LocationFinder extends Service {
    public static double lat, lng;

    LocationManager locationManager;

    public void onDestroy() {

        super.onDestroy();

        if (locationManager != null && locationListener != null) {
            locationManager.removeUpdates(locationListener);
        }

    }

    @Override
    public void onCreate() {
        Log.v("location", "===>location ed  onCreate " + lat);

        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        Log.v("location", "===>location ed  onStartCommand " + lat + "==>" + lng);
        new Handler().post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                getLocation();

            }
        });
        return START_STICKY;
    }

    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

    private void getLocation() {
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getSystemService(context);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        if (locationManager != null) {
            String provider = locationManager.getBestProvider(criteria, true);
            if (provider != null) {
                Location location = locationManager.getLastKnownLocation(provider);
                updateWithNewLocation(location);
                locationManager.requestLocationUpdates(provider, 500, 50, locationListener);
            } else {
                if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 50, locationListener);

                } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 50, locationListener);
                } else if (locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
                    locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 500, 50, locationListener);
                }
            }

        }
    }

    private void updateWithNewLocation(Location location) {
        if (location != null) {
            Log.v("location", "===>location ed  " + lat);

            lat = location.getLatitude();
            lng = location.getLongitude();

        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}