Android UI线程任务导致应用程序冻结

时间:2018-03-08 21:09:17

标签: android

下面的代码应该每秒跟踪用户的位置,在地图上添加一个标记,然后将位置添加到latlng坐标集的arraylist中。

现在,当按下“跟踪”按钮时,应用程序只会冻结。

它与我使用UI线程的方式有关吗?

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        return;
    }
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this can be null.
                    if (location == null) return;
                    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                    mMap.addMarker(new MarkerOptions().position(latLng).title("Current Location"));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f));
                }
            });
    trackButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                paths.add(new ArrayList<LatLng>());
                tracking = true;
                MapsActivity.this.runOnUiThread(new Runnable() {
                    @SuppressLint("MissingPermission")
                    public void run() {
                        final ArrayList currpath = paths.get(paths.size()-1);
                        PolylineOptions currpathlineoptions = new PolylineOptions();
                        final Polyline currpathline = mMap.addPolyline(currpathlineoptions);
                        while (tracking) {
                            mFusedLocationClient.getLastLocation()
                                    .addOnSuccessListener(MapsActivity.this, new OnSuccessListener<Location>() {
                                        @Override
                                        public void onSuccess(Location location) {
                                            // Got last known location. In some rare situations this can be null.
                                            if (location == null) return;
                                            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f));
                                            currpath.add(latLng);
                                            currpathline.setPoints(currpath);
                                        }
                                    });
                            SystemClock.sleep(1000);
                        }
                    }
                });

            } else {
                tracking = false;
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

你基本上是用循环中的动作充斥UI线程,所以难怪没有任何反应。你可以在调用onSuccess后每次发布延迟1000的动作,例如:

java.lang