簡體   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