2017-01-02 2 views
1

Так, как видно из заголовка, по какой-либо причине клиент API Google Google застрял в состоянии соединения без ошибок в информации об отладке. Я подождал до 5 минут для ответа.GoogleApiClient застрял в состоянии соединения, об ошибке не сообщается отладкой

Вот код для отслеживания моего текущего местоположения.

public class myLocation implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     ActivityCompat.OnRequestPermissionsResultCallback, 
     LocationListener 
{ 


    public static final String TAG = myLocation.class.getSimpleName(); 

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
    private final static int MY_PERMISSION_ACCESS_FINE_LOCATION = 1; 

    //global variables for information passed on from parent class 
    public GoogleApiClient mGoogleApiClient = null; 
    private Context mParentBase; 
    private Activity mParentActivity; 
    private PendingIntent mParentIntent; 

    private LocationRequest requestLocation; 

    public Location mMyLocation; 

    /** 
    * Constructor For this class 
    */ 
    public myLocation(Context Base, Activity activitiyBase, PendingIntent intent) { 
     mParentBase = Base; 
     mParentActivity = activitiyBase; 
     mParentIntent = intent; 
     initialLocationService(); 
    } 

    /** 
    * Initialize the Location Service and API Client 
    */ 

    public void initialLocationService(){ 
     mGoogleApiClient = new GoogleApiClient.Builder(mParentBase) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 

     requestLocation = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setInterval(10*1000) 
      .setFastestInterval(1*1000); 

     Log.d(TAG, "Initialization Location Service"); 
    } 

    public void connect() 
    { 
     mGoogleApiClient.connect(); 
     Log.d(TAG, "Connection Requested"); 
    } 

    public void pause() { 
     if (mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mParentIntent); 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    public void disconnect() { 
     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 

     Log.d(TAG, "OnConnected"); 

     if (PackageManager.PERMISSION_GRANTED == 
       (ContextCompat.checkSelfPermission(mParentBase, android.Manifest.permission.ACCESS_FINE_LOCATION))) 

     { 
      Log.d(TAG, "Permission Was Granted"); 
      mMyLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

      if (mMyLocation == null) { 
       LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, requestLocation, mParentIntent); 
      } else { 
       Log.d(TAG, mMyLocation.toString()); 
      } 
     } else { 
      ActivityCompat.requestPermissions(mParentActivity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_ACCESS_FINE_LOCATION); 
      Log.d(TAG, "Permission Was Requested"); 
     } 

    } 


    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], 
              @NonNull int[] grantResults) 
    { 
     switch (requestCode) { 
      case MY_PERMISSION_ACCESS_FINE_LOCATION: { 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        if (PackageManager.PERMISSION_GRANTED == 
          (ContextCompat.checkSelfPermission(mParentBase, android.Manifest.permission.ACCESS_FINE_LOCATION))) 

        { 
         mMyLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

         if (mMyLocation == null) { 
          LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, requestLocation, mParentIntent); 
         } else { 
          Log.d(TAG, mMyLocation.toString()); 
         } 
        } else { 
         ActivityCompat.requestPermissions(mParentActivity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
           MY_PERMISSION_ACCESS_FINE_LOCATION); 
        } 

       } 
      } 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.i(TAG, "Location services suspended. Please Reconnect."); 
    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

     Log.i(TAG, "Connection Failed"); 

     if (connectionResult.hasResolution()) { 
      try { 
       // Start an Activity that tries to resolve the error 
       connectionResult.startResolutionForResult(mParentActivity, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
      } catch (IntentSender.SendIntentException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 

    } 

    public void onLocationChanged(Location location) { 
     mMyLocation = location; 
     Log.d(TAG, location.toString()); 
    } 
} 

Вот код, который вызывает мой объект местоположения:

public class selectRouteAndTransportMethod extends AppCompatActivity { 

    Intent mIntent; 
    PendingIntent mPendingIntent; 
    myLocation mCurrentLocation; 
    public static final String TAG = selectRouteAndTransportMethod.class.getSimpleName(); 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_select_route_and_transport_method); 

     mIntent = new Intent(this, selectRouteAndTransportMethod.class); 
     mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0); 
     mCurrentLocation = new myLocation(this, this, mPendingIntent); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 

     // See https://g.co/AppIndexing/AndroidStudio for more information. 

    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 

     Log.d(TAG, "On Start"); 

     while (mCurrentLocation.mGoogleApiClient == null) { 
      Log.d(TAG, "Google ApiClient Not Instantiated"); 
     } 
     mCurrentLocation.connect(); 

     while (true) { 
      boolean connecting = mCurrentLocation.mGoogleApiClient.isConnecting(); 
      boolean registered = mCurrentLocation.mGoogleApiClient.isConnectionCallbacksRegistered(mCurrentLocation); 
      //ConnectionResult connectionResult = mCurrentLocation.mGoogleApiClient.getConnectionResult(LocationServices.API); 

      if (connecting == TRUE) { 
       Log.d(TAG, "Connecting"); 
      } 

      if (registered == TRUE) { 
       Log.d(TAG, "registered"); 
      } 

       // Log.d(TAG, connectionResult.toString()); 

      if (mCurrentLocation.mMyLocation != null) { 
       break; 
      } 
     } 
    } 
} 

Вот выход из журнала отладки.

D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
D/selectRouteAndTransportMethod: registered 
D/selectRouteAndTransportMethod: Connecting 
+0

Вы заменили цикл while? – noogui

ответ

1

Я думаю, что это как-то связано с вашей петлей while(true) {}. Попробуйте изменить это на if-statement. Что происходит, вы только что поставили себя в бесконечное затруднительное положение. Вы можете увидеть доказательство бесконечного цикла в вашем журнале отладки.

+0

Большое вам спасибо, но какая вещь, которая меня озадачивает, я думал, соединение API-клиента было асинхронным, а что не является частью основного потока, было ли мое предположение неправильным? – law

Смежные вопросы