实施 GoogleApiClient 时遇到问题
Posted
技术标签:
【中文标题】实施 GoogleApiClient 时遇到问题【英文标题】:Trouble Implementing GoogleApiClient 【发布时间】:2015-12-11 03:08:18 【问题描述】:我正在尝试在我正在创建的应用上使用 Google 登录。我一直在使用 Google 的开发人员教程,但我收到错误“必须将类 'HomeActivity' 声明为抽象或在 'ConnectionCallbacks' 中实现抽象方法 'onConnected(Bundle)',如下所示:
public class HomeActivity extends Activity implements
ConnectionCallbacks,
OnConnectionFailedListener,
OnClickListener
这是我的 HomeActivity 代码,其中大部分来自 Google:
import android.app.Activity;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
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.common.api.Scope;
import com.google.android.gms.plus.Plus;
import static android.view.View.*;
public class HomeActivity extends Activity implements
ConnectionCallbacks,
OnConnectionFailedListener,
OnClickListener
private static final int RC_SIGN_IN = 0;
private GoogleApiClient mGoogleApiClient;
private Button mLoginButton;
private TextView mSignUpLabel;
/* Is there a ConnectionResult resolution in progress? */
private boolean mIsResolving = false;
/* Should we automatically resolve ConnectionResults when possible? */
private boolean mShouldResolve = false;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mLoginButton = (Button) findViewById(R.id.btnLogin);
mLoginButton.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(HomeActivity.this, "Button Pressed", Toast.LENGTH_SHORT).show();
);
mSignUpLabel = (TextView) findViewById(R.id.lblAboutUs);
mSignUpLabel.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(HomeActivity.this, "WAY TO SIGN UP MAN", Toast.LENGTH_SHORT).show();
);
findViewById(R.id.sign_in_button).setOnClickListener(this);
// Build GoogleApiClient with access to basic profile
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
.build();
@Override
public void onConnectionSuspended(int arg0)
// what should i do here ? should i call mGoogleApiClient.connect() again ? ?
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
// Could not connect to Google Play Services. The user needs to select an account,
// grant permissions or resolve an error in order to sign in. Refer to the javadoc for
// ConnectionResult to see possible error codes.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
if (!mIsResolving && mShouldResolve)
if (connectionResult.hasResolution())
try
connectionResult.startResolutionForResult(this, RC_SIGN_IN);
mIsResolving = true;
catch (IntentSender.SendIntentException e)
Log.e(TAG, "Could not resolve ConnectionResult.", e);
mIsResolving = false;
mGoogleApiClient.connect();
else
// Could not resolve the connection result, show the user an
// error dialog.
showErrorDialog(connectionResult);
else
// Show the signed-out UI
showSignedOutUI();
@Override
protected void onStart()
super.onStart();
mGoogleApiClient.connect();
@Override
protected void onStop()
super.onStop();
mGoogleApiClient.disconnect();
任何帮助将不胜感激!!!
【问题讨论】:
【参考方案1】:您的类声明它将实现ConnectionCallbacks
,这意味着您必须定义该接口中包含的所有方法(以及您希望实现的所有其他接口)。两者都有
Android Studio(点击左端的灯泡)和 Eclipse 必须能够自动实现未实现的方法(您当然必须用任何自定义代码填充它们)
【讨论】:
哇,我也完全知道。太感谢了!这是一个很大的帮助!以上是关于实施 GoogleApiClient 时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章
<Your App> 在使用 Google Play 服务时遇到问题 - 使用 GoogleApiClient 时,它总是会触发 onConnectionFailed
谷歌排行榜提交分数错误:必须连接GoogleApiClient
GoogleApiClient.isConnected() 在飞行模式下返回 true