Xamarin Android谷歌登录连接回调从未抛出
我正在尝试在我的Xamarin Android应用程序中实现Google+登录。我刚得到一个活动,它显示了一个登录的Google+按钮。但是ApiClient从不抛出OnConnected回调 我实现了以下接口:Xamarin Android谷歌登录连接回调从未抛出,android,xamarin,xamarin.android,google-plus,Android,Xamarin,Xamarin.android,Google Plus,我正在尝试在我的Xamarin Android应用程序中实现Google+登录。我刚得到一个活动,它显示了一个登录的Google+按钮。但是ApiClient从不抛出OnConnected回调 我实现了以下接口: GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener 在OnCreate中,我初始化了Google的东西: var googleLogin = FindViewById<
GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener
在OnCreate中,我初始化了Google的东西:
var googleLogin = FindViewById<SignInButton>(Resource.Id.google_login);
googleLogin.SetSize(SignInButton.SizeWide);
googleLogin.Click += GoogleLogin_Click;
_googleApiClient = new GoogleApiClient.Builder(this)
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(PlusClass.API)
.AddScope(new Scope(Scopes.Profile))
.Build();
然后我想我可以通过这种方式获得用户信息:
public void OnConnected(Bundle connectionHint)
{
var people = PlusClass.PeopleApi.GetCurrentPerson(_googleApiClient);
}
提示:GetCurrentPerson被标记为已弃用。如果有人知道如何用现代方式解决此问题,请告诉我:)
在OnStop()中,我只是断开了客户端的连接。但我的问题是,从未调用OnConnected。我错过什么了吗?我确实在开发者控制台注册了这个应用。有几个线程可以执行此操作,但到目前为止没有任何帮助:(正在尝试将范围更改为
DriveClass.ScopeFile
:
_googleApiClient = new GoogleApiClient.Builder(Application.Context)
.UseDefaultAccount()
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(PlusClass.API)
.AddScope(PlusClass.ScopePlusLogin)
.Build();
这应该得到Connect
来调用您的OnConnectionFailed
回调,您可以在那里处理ConnectionResult
并且在用户选择一个帐户后,您的OnConnected
中的签名最终将被调用
更新:
手动管理的连接示例:
public类MainActivity:Activity,GoogleAppClient.IConnectionCallbacks,GoogleAppClient.IOnConnectionFailedListener
{
谷歌客户;
布尔分解误差;
const string TAG=“MyGPlus”;
const int REQUEST_RESOLVE_ERROR=999;
未连接的公共无效(捆绑连接提示)
{
调试(标记“OnConnected”);
}
连接失败的公共void(连接结果)
{
调试(标记“OnConnectionFailed”);
如果(_resolvingError)
返回;
if(result.hassolution)
{
尝试
{
_resolvingError=true;
result.StartResolutionForResult(这是请求\u解决\u错误);
}
catch(IntentSender.sendtintentexe)
{
Log.Debug(标记,e.Message);
client.Connect();
}
}
其他的
{
错误对话框(result.ErrorCode);
}
}
无效错误对话框(int errorCode)
{
var dialogFragment=新的ErrorDialogFragment();
var args=new Bundle();
args.PutInt(“对话框错误”,错误代码);
dialogFragment.Arguments=args;
显示(FragmentManager,“errordialog”);
}
连接上的公共无效已暂停(内部原因)
{
调试(标记“OnConnectionSuspended”);
}
受保护的覆盖void OnActivityResult(int请求代码、结果代码、Android.Content.Intent数据)
{
base.OnActivityResult(请求代码、结果代码、数据);
if(requestCode==请求\解决\错误)
{
if(resultCode==Result.Ok)
{
如果(!client.IsConnecting&&!client.IsConnected)
{
client.Connect();
}
}
else if(resultCode==Result.cancelled)
{
Log.Debug(标记“Result.cancelled”);
}
}
}
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
Button Button=FindViewById(Resource.Id.myButton);
按钮。单击+=委派
{
client=new GoogleAppClient.Builder(Application.Context)
.UseDefaultAccount().EnableAutoManage()
.AddConnectionCallbacks(此)
.AddOnConnectionFailedListener(此)
.AddApi(PlusClass.API)
.AddScope(PlusClass.ScopePlusLogin)
.Build();
_resolvingError=false;
client.Connect();
};
}
}
OnConnectionFailed现在被调用:)。但我如何触发登录?虽然谷歌会帮我做这件事…@Sebi不,你必须处理它,除非你通过构建者的EnableAutoManage
使用一个自动管理的连接,并实施了FragmentActivity
。。。我将在答案中添加一个手动管理的连接示例…非常感谢。现在,用户可以选择一个帐户。我的OnActivityResult方法始终得到结果。即使我使用我的帐户登录,也已取消。。。因此,未进一步到达OnConnected事件。@Sebi如果您得到的是结果。已取消
,则您的应用程序可能未使用GPlus API在Google API控制台中注册,并且/或者您的应用程序未使用您告诉控制台您正在使用的密钥库(通过SHA1密钥)签名.@Sebi假设您使用的是Xamarin的默认调试密钥,请确保控制台中使用了SHA1(请参阅链接)。。。您可以稍后将其更新为自定义发布密钥库,以提交到Play Store。。。
_googleApiClient = new GoogleApiClient.Builder(Application.Context)
.UseDefaultAccount()
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(PlusClass.API)
.AddScope(PlusClass.ScopePlusLogin)
.Build();
public class MainActivity : Activity, GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener
{
GoogleApiClient client;
bool _resolvingError;
const string TAG = "MyGPlus";
const int REQUEST_RESOLVE_ERROR = 999;
public void OnConnected(Bundle connectionHint)
{
Log.Debug(TAG, "OnConnected");
}
public void OnConnectionFailed(ConnectionResult result)
{
Log.Debug(TAG, "OnConnectionFailed");
if (_resolvingError)
return;
if (result.HasResolution)
{
try
{
_resolvingError = true;
result.StartResolutionForResult(this, REQUEST_RESOLVE_ERROR);
}
catch (IntentSender.SendIntentException e)
{
Log.Debug(TAG, e.Message);
client.Connect();
}
}
else
{
ShowErrorDialog(result.ErrorCode);
}
}
void ShowErrorDialog(int errorCode)
{
var dialogFragment = new ErrorDialogFragment();
var args = new Bundle();
args.PutInt("dialog_error", errorCode);
dialogFragment.Arguments = args;
dialogFragment.Show(FragmentManager, "errordialog");
}
public void OnConnectionSuspended(int cause)
{
Log.Debug(TAG, "OnConnectionSuspended");
}
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_RESOLVE_ERROR)
{
if (resultCode == Result.Ok)
{
if (!client.IsConnecting && !client.IsConnected)
{
client.Connect();
}
}
else if (resultCode == Result.Canceled)
{
Log.Debug(TAG, "Result.Canceled");
}
}
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate
{
client = new GoogleApiClient.Builder(Application.Context)
.UseDefaultAccount() .EnableAutoManage()
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(PlusClass.API)
.AddScope(PlusClass.ScopePlusLogin)
.Build();
_resolvingError = false;
client.Connect();
};
}
}