onLocationChanged方法仍在不同的Activity中运行

时间:2017-09-07 22:15:55

标签: java android android-intent android-context

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

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

所有在原始活动的onCreate内(DriverChoicesActivity.class)。除了从此活动获取意图以收集一些纬度和经度点之外,其他活动(DriverMapActivity.class)中没有代码。这是产生意图的代码(也在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);
                    }
                }
            }
        }
}); 

我认为我的问题是以某种方式与上下文有关。如果有人能够善意解释。

谢谢

1 个答案:

答案 0 :(得分:1)

您必须添加:

locationManager.removeUpdates(listener);

在转到下一个活动之前。

LocationManager locationManager;
LocationListener locationListener;

在顶部,在类声明