onLocationChanged方法

onLocationChanged方法

我有一个使用onLocationChanged方法的活动,并且在执行解析查询后会吐司。它工作正常。但是,当我进行另一项活动(这是地图活动)时,如果更改坐标(我正在使用模拟器),则会弹出吐司。我想知道为什么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) {
        }
};


原始活动(DriverChoicesActivity.class)的onCreate中的所有内容。另一个活动(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);
                    }
                }
            }
        }
});


我认为我的问题在于上下文。如果有人足够善良地解释。

谢谢

最佳答案

您必须添加:

locationManager.removeUpdates(listener);

在转到下一个活动之前。



LocationManager locationManager;
LocationListener locationListener;


在顶部,在类声明下

关于java - onLocationChanged方法仍在不同的Activity中运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46105864/

10-10 10:14