保存 GPS 和启用按钮的实例状态
Save Instance State of GPS and enabled buttons
我的问题是,当我旋转 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);
}
我不太明白你的问题,但如果你想在屏幕旋转时保存一些东西,你可以检查屏幕旋转,如下所示:
@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">
希望对您有所帮助!
需要在 onPause()
中添加这个。可以解释一下。
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
stopLocationUpdates();
}
}
我的问题是,当我旋转 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);
}
我不太明白你的问题,但如果你想在屏幕旋转时保存一些东西,你可以检查屏幕旋转,如下所示:
@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">
希望对您有所帮助!
需要在 onPause()
中添加这个。可以解释一下。
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
stopLocationUpdates();
}
}