2016-11-07 72 views
0

我正在關注Google的教程,關於如何使用GoogleApiClient獲取我的Android應用中的位置,但是我在構建客戶端並嘗試連接後遇到了問題。我看到客戶端正在嘗試連接,但onConnected()onConnectionFailed()回調都不會被調用。因此,客戶永遠不會建立連接或報告他未能這樣做。我在這裏錯過了什麼?Android的GoogleApiClient的onConnect()回調永遠不會被調用

更新:添加了訪問位置的權限檢查。

public class MainActivity extends AppCompatActivity 
    implements OnMapReadyCallback, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener { 

public static final String LOG_TAG = MainActivity.class.getSimpleName(); 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
private final static int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 0; 

private Location mCurrentLocation = new Location("default"); 
private GoogleApiClient mGoogleApiClient = null; 
private Location mLastLocation = null; 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 
       Log.e("Connected: ", String.valueOf(mGoogleApiClient.isConnected())); 
       mGoogleApiClient.connect(); 
       Log.e("Connected: ", String.valueOf(mGoogleApiClient.isConnected())); 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 
    } 
} 

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

    // check if permissions are granted 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     ActivityCompat.requestPermissions(this, 
       new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
       MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 

    } 

    if (mGoogleApiClient == null) { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 
} 


@Override 
protected void onStop() { 
    if (mGoogleApiClient.isConnected()) { 
     mGoogleApiClient.disconnect(); 
    } 

    super.onStop(); 
} 

private void handleNewLocation(Location location) { 
    Log.d(LOG_TAG, location.toString()); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    try { 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (mLastLocation == null) { 

     } 
     else { 
      handleNewLocation(mLastLocation); 
     } 
    } catch (SecurityException e) { 
     DialogFragment mlocationDialog = new LocationDialogFragment(); 
     mlocationDialog.show(getSupportFragmentManager(), "locationDialog"); 
    } 

} 

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

回答

0

您是否實施了權限?

@Override 
public void onRequestPermissionsResult(int requestCode, 
    String permissions[], int[] grantResults) { 
    switch (requestCode) { 
    case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
     // If request is cancelled, the result arrays are empty. 
     if (grantResults.length > 0 
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

      // permission was granted, yay! Do the 
      // contacts-related task you need to do. 

     } else { 

      // permission denied, boo! Disable the 
      // functionality that depends on this permission. 
     } 
     return; 
    } 

    // other 'case' lines to check for other 
    // permissions this app might request 
} 
} 

更多:https://developer.android.com/training/permissions/requesting.html

+0

是的,我沒有嘗試,但發現該權限已發,所以這不是問題。 – martasd

+0

手動管理連接時,您需要在應用生命週期的正確位置調用connect()和disconnect()方法。在活動上下文中,最佳做法是在您的活動的onStart()方法中調用connect(),並在您的活動的onStop()方法中調用disconnect()。當使用自動管理連接時,會自動調用connect()和disconnect()方法。 –

相關問題