0

我正在檢查位置服務是否在移動設備上啓用,如果是,請監聽構建googleapiclient並偵聽位置更改。我面臨的問題是,一旦位置在移動和可控制的位置轉動,我無法獲取位置(緯度/長度)。該位置始終是null位置爲null融合位置提供者android

isLocationEnabled - 如果檢查位置打開

  if (isLocationEnabled(getApplicationContext())) { 
       mMenu.getItem(0).setVisible(true); 
      } 
      else { 
       Snackbar.make(mLayout, getString(R.string.loc_not_enable), 
         Snackbar.LENGTH_LONG).show(); 
      } 
     } 
    }); 
    //setup GoogleApiclient 
    buildGoogleApiClient(); 


public static boolean isLocationEnabled(Context context) { 
    int locationMode = 0; 
    String locationProviders; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ 
     try { 
      locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE); 
     } catch (SettingNotFoundException e) { 
      e.printStackTrace(); 
      return false; 
     } 
     return locationMode != Settings.Secure.LOCATION_MODE_OFF; 

    }else{ 
     locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     return !TextUtils.isEmpty(locationProviders); 
    } 
} 


private synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
} 

@Override 
protected void onStart() { 
    // Connect the client. 
    super.onStart(); 
    if (mGoogleApiClient != null) { 
     mGoogleApiClient.connect(); 
    } 
} 

@Override 
protected void onStop() { 
    // Disconnecting the client invalidates it. 
    mGoogleApiClient.disconnect(); 
    super.onStop(); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_LOCATION); 
    } else { 
     startLocationServices(); 

    } 
} 

private void startLocationServices() { 
    try { 
     LocationRequest mLocationRequest = LocationRequest.create(); 
     Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation==null){ 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } else { 
      Log.d("mLastLocation", String.valueOf(mLastLocation)); 
      lat = String.valueOf(mLastLocation.getLatitude()); 
      lon = String.valueOf(mLastLocation.getLongitude()); 
     } 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(10*1000); // Update location every 10 seconds 
     mLocationRequest.setFastestInterval(1 * 1000); 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } catch (SecurityException exception) { 
     exception.printStackTrace(); 
    } 
} 


    E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.my.game.wesport, PID: 30629 
       java.lang.NumberFormatException: empty String 
        at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071) 
        at java.lang.Double.parseDouble(Double.java:547) 
        at com.my.game.wesport.MapsActivity.onMapReady(MapsActivity.java:161) 
        at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) 
        at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source) 
        at android.os.Binder.transact(Binder.java:499) 
        at zu.a(:com.google.android.gms.DynamiteModulesB:82) 
        at maps.ad.t$5.run(Unknown Source) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

回答

0

後,我重新啓動我的電話就解決了這個問題。

相關問題