获取地点(坐标)没有定期大幅增加电池消耗 [英] Get location (coordinates) periodically without dramatically increase battery consumption

查看:230
本文介绍了获取地点(坐标)没有定期大幅增加电池消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序;这个应用程序需要发送定期(每10分钟)的当前位置(coordenates)发送到Web服务。但是...我了解更多的正确方法(和友好的设备电池)有点糊涂做到这一点。

I'm developing an Android application; this App needs to send periodically (every 10 minutes) the current position (coordenates) to a web service. But ... I'm a little confused about the more correct way (and friendlier to the device battery) to do that.

我读到这回答和她的方法 _getLocation()看起来好;但我不知道该方法是否能得到我需要的位置的可用性;总可用性...

I read this answer and her method _getLocation() looks well; but I don't know whether that method could get the availability of the location I need; total availability...

我想,如果位置使用GSM / WIFI,应用选择GPS方法不可用。

I would like, if the location is not available using GSM / WIFI, application choose the GPS method.

是什么让这个方法?

private void _getLocation() {
    // Get the location manager
    LocationManager locationManager = (LocationManager) 
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }
}

有人知道的一种方式,定期获得该设备的坐标......没有显着增加电池消耗?

Anybody know one way to get the coordinates of the device periodically... without dramatically increase battery consumption?

推荐答案

如果你担心电池没那么严格的间隔10分钟,你可以尝试使用PassiveProvider替代GPS /粗。结果
通常一些应用程序要求的位置往往不够,所以你不必担心。结果
如果你是严格的比你可以试着问自己定位在一个没有过去的时间间隔收到这样。结果
这里是使用被动提供一个例子。结果

If you are worried about battery and no so strict on the 10 minutes interval you could try using the PassiveProvider instead of GPS/Coarse.
Usually some applications request locations often enough so you don't need to worry about it.
If you are strict than you could try to ask for location yourself in case one hasn't received in the past interval.
Here is an example for using the Passive Provider.

LocationManager locationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {

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

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onLocationChanged(Location location) {
        // Do work with new location. Implementation of this method will be covered later.
        doWorkWithNewLocation(location);
    }
};

long minTime = 10*60*1000;
long minDistance = 0;

locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, minTime, minDistance, locationListener);

这篇关于获取地点(坐标)没有定期大幅增加电池消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆