onLocationChanged 方法仍然 运行 不同 Activity

onLocationChanged method still running in different Activity

我有一个 activity,它使用 onLocationChanged 方法,在执行解析查询时它会做一个 toast。它工作正常。但是,当我转到另一个 activity(它是一个地图 activity)时,如果我更改坐标(我使用的是模拟器),则会弹出 toast。我想知道为什么 onLocationChanged 方法仍然是 运行。我认为这可能是由于上下文,但我在上下文字段中指定了 activity。

locationManager = (LocationManager) DriverChoicesActivity.this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            final ParseGeoPoint parseGeoPoint = new ParseGeoPoint(location.getLatitude(), location.getLongitude());
            ParseQuery<ParseUser> query = ParseUser.getQuery();
            query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
            query.findInBackground(new FindCallback<ParseUser>() {
                @Override
                public void done(List<ParseUser> objects, ParseException e) {
                    if (e == null && objects.size() > 0) {
                        for (ParseObject object : objects) {
                            object.put("driverLocation", parseGeoPoint);
                            object.saveInBackground();
                            Toast.makeText(DriverChoicesActivity.this, "User found", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });

            updateRequestList(location);

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

原始 activity (DriverChoicesActivity.class) 的 onCreate 中的所有内容。另一个 activity (DriverMapActivity.class) 除了从这个 activity 获得收集一些纬度和经度点的意图之外没有代码。这是制作意图的代码(也在 onCreate 中)

requestListView.setAdapter(arrayAdapter);
requestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (requestLatitude.size() > position && requestLongitude.size() > position) {
                if (Build.VERSION.SDK_INT < 23 || ContextCompat.checkSelfPermission(DriverChoicesActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    Location getLastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (getLastKnownLocation != null) {
                        Intent intent = new Intent(getApplicationContext(), DriverMapActivity.class);
                        intent.putExtra("requestLatitude", requestLatitude.get(position));
                        intent.putExtra("requestLongitude", requestLongitude.get(position));
                        intent.putExtra("driverLatitude", getLastKnownLocation.getLatitude());
                        intent.putExtra("driverLongitude", getLastKnownLocation.getLongitude());
                        startActivity(intent);
                    }
                }
            }
        }
}); 

我想我的问题不知何故与上下文有关。如果有好心人解释一下。

谢谢

您必须添加:

locationManager.removeUpdates(listener);

在移动到下一个之前Activity。

LocationManager locationManager;
LocationListener locationListener;

在顶部,在 class 声明下