我想通過使用播放服務API獲取用戶的當前位置。但始終呼籲onConnectionFailed()
方法。我添加了帶有SHA-1密鑰的google-services.json文件。但是它顯示了用onConnectionFailed()方法編寫的Toast消息。我該如何解決它?Android - 無法連接谷歌播放服務
這裏是我的代碼 -
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//setup GoogleApiClient
setupGoogleApiClient();
}
}
}
//setup GoogleApiClient object
private void setupGoogleApiClient() {
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
}
protected void onStart() {
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Toast.makeText(this, "Connected with google play service", Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this, "Connection suspended with google play service", Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(this, "Failed to connect with google play service", Toast.LENGTH_LONG).show();
}
}
請發表您的代碼 –
添加的代碼@NK mewara –
你應該自己調試一下,看看有什麼特別的'ConnectionResult'來了。 – Beloo