0

My problem is that when i rotate the GPS, then I can't stop the LocationServices.FusedLocationApi. This only happens after I have rotated the screen. If I dont change the orientation the start and stop works fine.

This should controll if it should update or not

private boolean mRequestingLocationUpdates;

Here is the values I want to save

public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putBoolean(REQUESTING_LOCATION_UPDATES_KEY, mRequestingLocationUpdates);
        savedInstanceState.putBoolean(REQUESTING_SAVED_FIRSTTIME, isFirstTime);
        savedInstanceState.putString(LAST_UPDATED_TIME_STRING_KEY, mLastUpdateTime);
        super.onSaveInstanceState(savedInstanceState);
    }

    private void updateValuesFromBundle(Bundle savedInstanceState) {
        Log.i(TAG, "Updating values from bundle");
        if (savedInstanceState != null) {
            if (savedInstanceState.keySet().contains(REQUESTING_LOCATION_UPDATES_KEY)) {
                mRequestingLocationUpdates = savedInstanceState.getBoolean(REQUESTING_LOCATION_UPDATES_KEY);

                if (mProgressBar != null) {
                    mProgressBar.setVisibility(View.VISIBLE);
                }

                if (lightsOn != null) {
                    lightsOn.setVisibility(View.VISIBLE);
                }
            }


            if (savedInstanceState.keySet().contains(IS_FIRST_TIME_KEY)) {
                isFirstTime = savedInstanceState.getBoolean(IS_FIRST_TIME_KEY);
            }

            // Update the value of mLastUpdateTime from the Bundle and update the UI.
            if (savedInstanceState.keySet().contains(LAST_UPDATED_TIME_STRING_KEY)) {
                mLastUpdateTime = savedInstanceState.getString(LAST_UPDATED_TIME_STRING_KEY);
            }
        }
    }

START and STOP Location updates

protected void startLocationUpdates() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}


protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
}

onCreate With buttons to controll the start and stop

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainreal);

    // Starting the gps and location updates
    mStartTrackerButton = (Button) findViewById(R.id.buttonStart);
    mStartTrackerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainReal.this, "START", Toast.LENGTH_SHORT).show();

            // Set true, will generate a new date, and a new startpoint in database
            isFirstTime = true;

            if (mProgressBar != null) {
                mProgressBar.setVisibility(View.VISIBLE);
            }

            if (lightsOn != null) {
                lightsOn.setVisibility(View.VISIBLE);
            }

            mRequestingLocationUpdates = true;
            startLocationUpdates();
        }
    });


    // STOP the gps and location updates
    mStopTrackerButton = (Button) findViewById(R.id.buttonStop);
    mStopTrackerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (mProgressBar != null) {
                mProgressBar.setVisibility(View.GONE);
            }

            if (lightsOn != null) {
                lightsOn.setVisibility(View.GONE);
            }

            mRequestingLocationUpdates = false;
            stopLocationUpdates();
        }
    });


    // Setting up google api client
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();


    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(5 * 1000);  // 2 seconds, in milliseconds


    mRequestingLocationUpdates = false;

    // Update values using data stored in the Bundle.
    updateValuesFromBundle(savedInstanceState);
}
Rasmus Rajje Josefsson
  • 1,564
  • 2
  • 15
  • 33

2 Answers2

0

I don't exactly understand your question, but if you want to save something when the screen is rotated you could check for screen rotation, like this:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int orientation = newConfig.orientation;

    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        // Save something
    }  else (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // Save something
    }
}

Or if you just want to lock the screen altogther (which also might be useful), you could write that in your AndroidManifest.xml file, like so:

<activity
    android:name="MainActivity"
    android:screenOrientation="portrait"
    android:configChanges="keyboardHidden|orientation|screenSize">

Hope it helps!

AkashBhave
  • 749
  • 4
  • 12
  • Sorry was tierd or something yesterday when posted this, have updated the question, I hope you understand my real problem better now. Thanks for your time! – Rasmus Rajje Josefsson May 28 '16 at 11:44
  • If I understand right, this method should work. Just stop the`LocationServices.FusedLocationApi` in the `if` statement. – AkashBhave May 28 '16 at 12:03
  • Isn't it recommended of doing `onSavedInstanceState` before using `onConfigruationChange`? If you look here, at **Save the State of the Activity** they are using what im trying to do. https://developer.android.com/training/location/receive-location-updates.html – Rasmus Rajje Josefsson May 28 '16 at 14:18
  • If I start the application, press start (gps starts) if I press stop (gps stops). If i press start(gps starts) if I rotate and then press stop **(gps doesn't stop)** if I rotate back and press stop **(gps doesn't stop)** – Rasmus Rajje Josefsson May 28 '16 at 14:25
  • hmm it looks like it turns off the locationservice but the icon still appears in the bar above the toolbar. and the ` public void onLocationChanged(Location location) { ` keeps going – Rasmus Rajje Josefsson May 28 '16 at 14:35
0

Needed to add this inside onPause(). Could have some explanation about it.

  @Override
    protected void onPause() {
        super.onPause(); 
        if (mGoogleApiClient.isConnected()) {
            stopLocationUpdates();
        }  
     }
Rasmus Rajje Josefsson
  • 1,564
  • 2
  • 15
  • 33