繁体   English   中英

位置跟踪服务会降低应用速度

[英]Location Tracking Service slows app down

对于一个项目,我需要一个可在webview中打开网页并同时跟踪用户位置的应用程序,即使手机未激活或其他应用程序位于前面。

我得到了这个工作,一切都按预期进行。 位置被检索,并且服务器能够使用这些位置。

问题是,如果我在后台跟踪两个多小时后又切换回应用程序,一切都会变慢,并且Webview中的响应时间非常糟糕。

似乎位置服务正在减慢应用程序的速度。 在安装服务之前,此问题不存在。 我无法解释,是什么原因导致应用程序缺乏,也许有人可以帮助我。

这是我的位置服务的代码。 在Webview的onCreate中将其称为Intent。 将位置写入字符串缓冲区中,然后将其上载到服务器。 (忽略了一些空的覆盖功能)

public class MyLocationService extends Service {

    double latService;
    double lngService;
    long timeService;
    float accService;
    long oldtime;
    String hash = "";
    String buffer = "";
    private LocationManager lm;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final Handler handler = new Handler();
        final Runnable r = new Runnable() {
            public void run() {
                LocationUpdates();
                if ((timeService > 0) && (oldtime != timeService)) {
                    oldtime = timeService;
                    if (buffer.equals("")) {
                        buffer += latService + "," + lngService + "," + accService + "," + timeService;
                    } else {
                        buffer += ";" + latService + "," + lngService + "," + accService + "," + timeService;
                    }
                    AsyncHttpClient client = new AsyncHttpClient();
                    RequestParams params = new RequestParams();
                    params.put("d", buffer);
                    client.post("server.php", params, new TextHttpResponseHandler() {
                        @Override
                        public void onSuccess(int arg0, Header[] arg1, String arg2) {
                            System.out.println(arg2);
                            buffer = "";
                        }
                    });
                }
                handler.postDelayed(this, 15000);
            }
        };
        handler.postDelayed(r, 10);
        return Service.START_NOT_STICKY;
    }

    public void LocationUpdates() {
        locListener locList = new locListener();
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locList);
    }

    public class locListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            latService = location.getLatitude();
            lngService = location.getLongitude();
            timeService = Math.round(location.getTime() / 1000);
            accService = location.getAccuracy();
        }
    }
}

在此先感谢您的帮助。

我强烈建议将融合位置提供程序与PRIORITY_BALANCED_POWER_ACCURACY结合使用:

LocationRequest request = LocationRequest.create()
        .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
        .setInterval(POLLING_INTERVAL)
        .setFastestInterval(POLLING_INTERVAL/2)
        .setSmallestDisplacement(MIN_INTERVAL_METERS);

它旨在为您提供40米范围内的精度,电池耗电量<= 0.6%/小时。

还请记住,在不再需要时立即关闭位置监听。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM