我正在使用本教程,并且收到错误:对于onConnectionFailed类中的ConnectionResult类型,未定义方法getIntentSender()。 我想点击按钮
整合Google Plus以下是完整代码:
package com.example.demogoogleshare;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.PlusClient;
public class MainActivity extends Activity implements OnClickListener, ConnectionCallbacks, OnConnectionFailedListener {
private static final int RC_SIGN_IN = 0;
Button signIn_button;
GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
PlusClient mPlusClient;
/* Track whether the sign-in button has been clicked so that we know to resolve
* all issues preventing sign-in without waiting.
*/
private boolean mSignInClicked;
/* Store the connection result from onConnectionFailed callbacks so that we can
* resolve them when the user clicks sign-in.
*/
private ConnectionResult mConnectionResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, null).addScope(Plus.SCOPE_PLUS_LOGIN).build();
signIn_button = (Button) findViewById(R.id.signIn_button);
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.signIn_button) {
Toast.makeText(getApplicationContext(), "signIn", 1000).show();
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
if (!mIntentInProgress && result.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(result.getIntentSender(), RC_SIGN_IN, null, 0, 0, 0);
}
catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnected(Bundle connectionHint) {
// We've resolved any connection errors. mGoogleApiClient can be used to
// access Google APIs on behalf of the user.
}
@Override
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
}
答案 0 :(得分:1)
尝试以下代码:
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private static final int REQUEST_CODE_SHARE = 1000;
private PlusClient mPlusClient;
private PlusClient.Builder mPlusClientBuilder;
private PlusShare.Builder mPlusShareBuilder;
//In your onCreate method write below code:
mPlusClientBuilder = new Builder(this, this, this);
mPlusClientBuilder.setScopes(Scopes.PLUS_LOGIN, Scopes.PROFILE);
mPlusClient = mPlusClientBuilder.build();
mPlusClient.connect();
//OnActivityResult method:
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
mPlusClient.disconnect();
mPlusClient.connect();
} else if (requestCode == REQUEST_CODE_SHARE && responseCode == RESULT_OK) {
finish();
}
}
//OnConnectionFailed method:
@Override
public void onConnectionFailed(ConnectionResult result) {
if (result.hasResolution()) {
// The user clicked the sign-in button already. Start to resolve
// connection errors. Wait until onConnected() to dismiss the
// connection dialog.
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
mPlusClient.disconnect();
mPlusClient.connect();
}
}
}
//OnConnected method:
@Override
public void onConnected(Bundle arg0) {
String accountName = mPlusClient.getAccountName();
}
//SignIn Button click:
btnSignIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (!mPlusClient.isConnected() && btnSignIn.getText().equals(getString(R.string.signin))) {
mPlusClient.connect();
} else if (mPlusClient.isConnected() && btnSignIn.getText().equals(getString(R.string.signout))) {
mPlusClient.clearDefaultAccount();
mPlusClient.disconnect();
}
}
});
// Share on google plus button click event:
btnShare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mPlusClient.isConnected()) {
mPlusShareBuilder = new PlusShare.Builder(getApplicationContext());
mPlusShareBuilder.setType("text/plain");
mPlusShareBuilder.setText(IN_SHARE_MESSAGE + IN_SHARE_LINK);
mPlusShareBuilder.setContentUrl(Uri.parse(IN_SHARE_LINK));
// Intent shareIntent =
// PlusShare.Builder.from(YOUR ACTIVITY.this).setText().setType("text/plain").setContent(Uri.parse("http://example.com/cheesecake/lemon"))
// .getIntent();
Intent shareIntent = mPlusShareBuilder.getIntent();
startActivityForResult(shareIntent, REQUEST_CODE_SHARE);
} else {
//print failure message here
}
}
});