Context has changed, operation is not possible
Asked Answered
C

0

7

My app is using two separate Process. I am having all the Services in one Process and all other component in another Process. When I am trying to get the lastKnownLocation from the LocationManger when an Intent with action is received in public int onStartCommand(Intent intent, int flags, int startId) from an Activity, I am getting the Location null , when I see the LocationManager object in the debugger its showing that Context has changed, operation is not possible. Thanks for helpenter image description here. Here is the snippet of code-

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getAction();
        if (action != null) {
            if (action.equalsIgnoreCase(Synchronize.ACTION_UPDATE_LOCATION)) {
                Location location = getLastKnownLocation();
                if (location != null) {
                    new Synchronize(getApplicationContext()).
                            updateLocation(prefs.getInt(Utils.PROPERTY_USER_ID, 0),
                                    location.getLatitude(), location.getLongitude(),
                                    Utils.formatDateMilliSecondToUTC(location.getTime()));
                }
            } else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_CONTACTS)) {
                new Synchronize(getApplicationContext()).syncContacts();
            } else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_EVENTS)) {
                new Synchronize(getApplicationContext()).syncEvents();
            } else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_OFFERS)) {
                new Synchronize(getApplicationContext()).syncOffers();
            } else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_DISCOVERS)) {
                new Synchronize(getApplicationContext()).syncDiscoverEvents();
            }
        }
        return START_STICKY;
    }

    private Location getLastKnownLocation() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                    checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
        } else {
            return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }
        return null;
    }
Collinear answered 17/10, 2015 at 18:52 Comment(3)
Hello Sanjeet, How did you resolved this issue, I am also getting same while I am using Sync Adapter for syncing.Siberia
I moved all the code on single process and issue resolved automatically, I am not sure if this can help in your case.Collinear
I can not do this because Sync adapter works in different process .By the way Thanks for your response.Siberia

© 2022 - 2024 — McMap. All rights reserved.