Reputation: 2759
I read the other similar problems but couldn't find a solution.
I implemented this yesterday https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi and it was working fine. Today I have no idea what I messed up in the project. When I enter the fragment and the location is disabled the window to ask me to turn on location does pop up, but no matter how many times I click no it won't go away or if I click yes I'll get in logcat
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: FATAL EXCEPTION: main
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: Process: name.company.newapp, PID: 23652
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1000, result=-1, data=Intent { (has extras) }} to activity {name.company.newapp/name.company.newapp.HomeScreen}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:4054)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4097)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread.access$1400(ActivityThread.java:177)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1497)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5938)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.google.android.gms.internal.zzlh.zzb(Unknown Source)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.google.android.gms.internal.zzli.zzb(Unknown Source)
11-17 01:02:05.470 23652-23652/name.company.newapp E/AndroidRuntime: at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
Basically in onActivityResult the googleClient
is null. As I said yesterday was working. ..
TestLocation fragment:
public class TestLocation extends Fragment implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
public TestLocation() {
}
private static GoogleApiClient googleClient;
private Location mLastLocation;
private PendingResult<LocationSettingsResult> result;
private LocationRequest locationRequest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (googleClient==null) {
googleClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// Defines the xml file for the fragment
View view = inflater.inflate(R.layout.upload_activity_fragment, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@Override
public void onConnected(Bundle bundle) {
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
result = LocationServices.SettingsApi.checkLocationSettings(googleClient, builder.build());
if (result != null) {
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a optionsDialog.
try {
// Show the optionsDialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
if (status.hasResolution()) {
status.startResolutionForResult(getActivity(), 1000);
}
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the optionsDialog.
break;
}
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((resultCode == Activity.RESULT_OK) && (requestCode == 1000)) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleClient);
if (mLastLocation == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleClient, locationRequest, this);
} else {
Log.d("askj","not null");
}
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
Log.e("location", "finally");
}
@Override
public void onResume() {
super.onResume();
if (googleClient!=null) {
googleClient.connect();
}
}
@Override
public void onPause() {
super.onPause();
if (googleClient!=null) {
if (googleClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(googleClient, this);
googleClient.disconnect();
}
}
}
}
Upvotes: 1
Views: 1204
Reputation: 39191
From the Google API reference for the GoogleApiClient#disconnect()
method:
public abstract void disconnect ()
Closes the connection to Google Play services. No calls can be made using this client after calling this method.
You are disconnecting the client in the onPause()
method, so as soon as you call the startResolutionForResult()
, your GoogleApiClient
is no longer valid. You don't need to stop and re-start the client as you would unregister and re-register a BroadcastReceiver
. From the top of that same page:
You should instantiate a client object in your Activity's
onCreate(Bundle)
method and then callconnect()
inonStart()
anddisconnect()
inonStop()
, regardless of the state.
Upvotes: 2