2016-12-26 61 views
-1

我想通過使用播放服務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(); 
    } 
} 
+0

請發表您的代碼 –

+0

添加的代碼@NK mewara –

+0

你應該自己調試一下,看看有什麼特別的'ConnectionResult'來了。 – Beloo

回答

2

嘗試在你的gradle這個文件多變的打法服務版本, 如果您正在使用

compile 'com.google.android.gms:play-services:10.0.1' 

嘗試

compile 'com.google.android.gms:play-services:8.x.x' 

等組合。

注: -替換「X」用合適的值

+0

我正在使用'10 .0.1'。它出什麼問題了?你能解釋我嗎? –

+0

親切地更新谷歌播放服務,然後從您的SDK馬槽 – Mrinmoy

+0

我剛剛更新谷歌播放服務現在。但仍然是相同的錯誤。 –