Попробуйте следующий код:
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient.Builder(ThirtyArticleDetail.this)
.addConnectionCallbacks(ThirtyArticleDetail.this)
.addOnConnectionFailedListener(ThirtyArticleDetail.this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
btnsharegplus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (!mGoogleApiClient.isConnected()) {
// Set sharing so that the share is started in onConnected.
mSignInClicked= true;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
} else {
Intent shareIntent = new PlusShare.Builder(ThirtyArticleDetail.this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse(articleurl))
//.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
}
});
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
mSignInClicked = false;
// Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
Intent shareIntent = new PlusShare.Builder(this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse(articleurl))
//.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
if(!(mGoogleApiClient.isConnected()))
mGoogleApiClient.connect();
}
/**
* Sign-in into google
* */
/*private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
*/
/**
* Method to resolve any signin errors
* */
private void resolveSignInError() {
try {
if (mConnectionResult.hasResolution()) {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
}
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
catch (Exception e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
Прежде всего, пожалуйста, пройти через follwing ссылки и понять, что GoogleApiClient на самом деле:
https://developers.google.com/android/guides/api-client
http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/
https://developers.google.com/+/mobile/android/share/
EDIT
Не забудьте принять следующие полномочия по в Manifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
mGoogleApiClient? как я инициализирую это – droidev
Я пробовал ваш код, но он вызывает onConnectionFailure() и получает сообщение об ошибке, ошибка входа в систему, ошибка при подписании указанного метода, пожалуйста, выберите другую учетную запись. что это ? – droidev
, но это для правильного входа в систему? мне нужно реализовать все это для совместного использования? – droidev