我读了其他类似的问题,但找不到解决方案。
我昨天实施了这个https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi,它运行正常。今天我不知道我在项目中搞砸了什么。当我输入片段并且位置被禁用时,会弹出要求我打开位置的窗口,但无论我点击多少次没有它都不会消失,或者如果我点击是我会进入logcat < / p>
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)
基本上在onActivityResult中googleClient
为空。正如我昨天所说的那样。 ..
TestLocation片段:
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();
}
}
}
}
答案 0 :(得分:2)
来自GoogleApiClient#disconnect()
方法的Google API参考:
public abstract void disconnect()
关闭与Google Play服务的连接。调用此方法后,无法使用此客户端进行调用。
您正在使用onPause()
方法断开客户端,因此只要您致电startResolutionForResult()
,您的GoogleApiClient
就不再有效了。您无需停止并重新启动客户端,就像取消注册并重新注册BroadcastReceiver
一样。从同一页面的顶部开始:
您应该在Activity的
onCreate(Bundle)
方法中实例化一个客户端对象,然后在connect()
和onStart()
中disconnect()
中调用onStop()
,无论状态如何。