0

我的问题是,当我旋转 GPS 时,我无法停止LocationServices.FusedLocationApi. 这仅在我旋转屏幕后发生。如果我不改变方向,启动和停止工作正常。

这应该控制它是否应该更新

private boolean mRequestingLocationUpdates;

这是我要保存的值

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

开始和停止位置更新

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 用按钮控制开始和停止

@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);
}
4

2 回答 2

0

我不完全理解你的问题,但如果你想在屏幕旋转时保存一些东西,你可以检查屏幕旋转,如下所示:

@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
    }
}

或者,如果您只想完全锁定屏幕(这也可能有用),您可以将其写入AndroidManifest.xml文件中,如下所示:

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

希望能帮助到你!

于 2016-05-28T01:04:38.900 回答
0

需要在里面添加这个onPause()。可以对此进行一些解释。

  @Override
    protected void onPause() {
        super.onPause(); 
        if (mGoogleApiClient.isConnected()) {
            stopLocationUpdates();
        }  
     }
于 2016-05-28T15:09:37.593 回答