我正在开发一个Android应用程序,其中我使用融合API位置更新和待定意图。这是我到目前为止所取得的成就。我能够在前台和后台状态中监听应用程序的位置更新。我能够从前景状态停止位置更新,并且还可以从后台恢复应用程序到前台。但在应用程序重启状态的情况下,我无法停止位置更新,因为一旦我从后台杀死应用程序然后它丢失GoogleApiClient
对象,我无法删除更新。我按照以下方式尝试了它:
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_location);
updateStatusForLocationListening();
initUi();
// if condition to check whether already started listening for updates.
if (!isLocationUpdatesOn) {
initLocationListeningProcess();
}
registerLocationListener();
}
private void initLocationListeningProcess() {
createLocationPendingIntent();
initLocationListener();
createLocationRequest();
buildLocationSettingsRequest();
checkForLocationPermission();
}
private void createLocationPendingIntent() {
Intent intervalIntent = new Intent(this, LocationPendingIntentService.class);
locationPendingIntent = PendingIntent.getService(this, 1, intervalIntent, PendingIntent.FLAG_CANCEL_CURRENT);
}
private void initLocationListener() {
buildGoogleApiClient();
}
synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
private void createLocationRequest() {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(100); // Update location every second
}
protected void buildLocationSettingsRequest() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
}
protected void checkLocationSettings() {
PendingResult < LocationSettingsResult > result =
LocationServices.SettingsApi.checkLocationSettings(
mGoogleApiClient,
mLocationSettingsRequest
);
result.setResultCallback(this);
}
private void processStopLocation() {
// user clicks on stop button.
// Here it is loosing mGoogleApiClient if user kills the application from the background. It keeps listening for updates.
// But when try to stop it, its giving Null pointer exception.
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, locationPendingIntent);
unregisterLocationListener();
mGoogleApiClient.disconnect();
}
private void processStartLocation() {
// user click on start button.
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, locationPendingIntent);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
updateUI();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
public class LocationPendingIntentService extends IntentService {
public LocationPendingIntentService(String name) {
super(name);
}
public LocationPendingIntentService() {
super("");
}
@Override
protected void onHandleIntent(Intent intent) {
Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
if (location != null) {
Log.i("***********", "Lcation Received ");
Intent locationIntent = new Intent("LOCATION_UPDATES_AVAILABLE");
locationIntent.putExtra("location", location);
LocalBroadcastManager.getInstance(this).sendBroadcast(locationIntent);
}
}
}
所以在我的情况下,我从后台杀了应用程序并再次启动它。它显示位置更新。我试图阻止它。它为我提供了mGoogleApiClient
对象的空指针。我尝试为GoogleApiClient
创建新对象。但是一旦我创建了新对象,它就会停止以前开始的位置更新。这意味着我需要开始新的位置更新。有没有办法存储此对象或获取mGoogleApiClient
的当前活动对象。所以我可以阻止它。是否有任何错误的实施我正在追踪或其他我错过的东西?需要一些帮助。谢谢。