I am trying to connect my app to Google Play Services but, as I can check, it seems not to connect it properly. When I click the button which checks if it is connected it returns me that is not connected. However the app accesses to the Google Play Services and lets me to check which e-mail account to use.
Here is the code:
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
//GoogleApiClient
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
client = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.build();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Games.signOut(client);
TextView textView = (TextView) findViewById(R.id.helloworld);
//client.connect();
if(client.isConnected()){
textView.setText("Buttonconnected");
}else{
textView.setText("Buttonnotconnected");
}
}
});
if (client == null) {
}
}
@Override
public void onStart() {
super.onStart();
//Log.i("app","Lapp");
if (client.isConnected()) {
//TextView t = (TextView) findViewById(R.id.helloworld);
//t.setText("Connected");
}
/* GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(this);
if (resultCode == ConnectionResult.SUCCESS) {
Toast.makeText(this, "Google Play Works!!", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(this, "Google Play Services required!!", Toast.LENGTH_LONG).show();
}*/
if (client != null) {
client.connect();
}
if (client.isConnected()) {
TextView t = (TextView) findViewById(R.id.helloworld);
t.setText("Connected");
}
}
@Override
public void onStop() {
super.onStop();
}
public void onConnectionSuspended(int cause) {
// We are not connected anymore!
Log.i("app", "connectionsuspended");
}
public void onConnected(Bundle connectionHint) {
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// We tried to connect but failed!
TextView t;
// t = (TextView) findViewById(R.id.helloworld);
// t.setText("Failed");
try {
//result.startResolutionForResult(this, 49404);
result.startResolutionForResult(this, 1001);
Log.i("app", "show");
} catch (Exception e) {
Log.i("app", e.toString());
}
client.connect();
client.hashCode();
Toast.makeText(this, "Toasting", Toast.LENGTH_LONG).show();
if (client.isConnected()) {
Log.i("app", "worked");
t = (TextView) findViewById(R.id.helloworld);
t.setText("FailedConnected");
}
if (client.isConnecting()) {
t = (TextView) findViewById(R.id.helloworld);
t.setText("Connecting");
Log.i("app", "Failedtryingtoconnect");
}
}
}
This is my build.gradle:
....
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.google.android.gms:play-services:9.0.2'
}
and this is my Manifest.xml
....
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
...
Am I missing something? I am using for this testing the same account as I registered in Google Developers Console. Am I missing OAuth2 signup?