简体   繁体   English

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

[英]Location Tracking Service slows app down

for a project I needed an app that opens a webpage in a webview and at the same time tracks the users location, even if the phone is not active or another app is in front. 对于一个项目,我需要一个可在webview中打开网页并同时跟踪用户位置的应用程序,即使手机未激活或其他应用程序位于前面。

I got this working and everything works as it should. 我得到了这个工作,一切都按预期进行。 The location is retrieved and the server is able to work with the locations. 位置被检索,并且服务器能够使用这些位置。

The problem is, that if I switch back into the app after more than two hours of background tracking, everything is slowed down and the response time in the webview is very bad. 问题是,如果我在后台跟踪两个多小时后又切换回应用程序,一切都会变慢,并且Webview中的响应时间非常糟糕。

It seems as if the location service is slowing down the app. 似乎位置服务正在减慢应用程序的速度。 Before the service was installed this problem did not exist. 在安装服务之前,此问题不存在。 I cant explain, what causes the app to lack, maybe somebody can help me. 我无法解释,是什么原因导致应用程序缺乏,也许有人可以帮助我。

This is the code of my location service. 这是我的位置服务的代码。 It gets called as an Intent in the onCreate of the Webview. 在Webview的onCreate中将其称为Intent。 The Locations gets written in a string buffer that gets uploaded to a server. 将位置写入字符串缓冲区中,然后将其上载到服务器。 (Some empty override functions are left out) (忽略了一些空的覆盖功能)

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();
        }
    }
}

Thanks in advance for any help. 在此先感谢您的帮助。

I would highly recommend using the Fused location provider with PRIORITY_BALANCED_POWER_ACCURACY: 我强烈建议将融合位置提供程序与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);

It is designed to provide you with a in a 40 meter range accuracy with battery consumption <= 0.6%/hour. 它旨在为您提供40米范围内的精度,电池耗电量<= 0.6%/小时。

Also remember to turn location listening off immediately when no longer needed. 还请记住,在不再需要时立即关闭位置监听。

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

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