适用于Android的Google登录:无法解析RC_SIGN_IN [英] Google Sign In for Android: Cannot resolve RC_SIGN_IN
问题描述
我正在尝试通过移动应用向后端服务器进行身份验证.
我正在关注此文档. https://developers.google.com/identity/sign-in/android /登录
但是,有一些错误. RC_SIGN_IN
和updateUI()
无法解析.
I'm trying to authenticate with backend server from mobile app.
I was following this documentation. https://developers.google.com/identity/sign-in/android/sign-in
However, there are some errors. RC_SIGN_IN
and updateUI()
cannot be resolved.
我的代码就是这样
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
mSignInButton = findViewById(R.id.sign_in_button);
mSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_LONG).show();
Intent signIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signIntent, RC_SIGN_IN);
}
});
@Override
protected void onStart() {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
super.onStart();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
String idToken = account.getIdToken();
// Send Id Token to the backend and validate here
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
更新
现在按钮本身不起作用.
Update
Now the button itself doesn't work.
xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Include the main content -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
<!-- Navigation bar -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
我该如何解决?
推荐答案
除了将RC_SIGN_IN
替换为int值外,您无需执行其他任何操作.它可以是任何值,但只能使用1作为其值.请执行以下操作:
You need to do nothing but replace RC_SIGN_IN
with an int value. It can be anything but use 1 as it's value. Do as follows:
startActivityForResult(signIntent, 1);
并按如下所示更改活动结果中的if代码:
And change the if code in activity result as follows:
if (requestCode == 1)
还将登录按钮点击代码更改为此(删除开关盒):
Also change the sign in button click code to this(remove switch cases):
mSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
}
});
这是因为您正在调用按钮的click方法,然后再次检查是否单击了同一按钮,这就是为什么我认为它不起作用的原因.
This is because you are calling on click method to the button and then again checking If the same button is clicked, that's why I think it's not working.
现在对于updateUI
方法,该方法应该由您定义.基本上,这是为了让您的应用更改用户登录后显示的内容.如果要在signedIn()
时打开新活动,可以通过将活动结果和onstart事件中的updateUI(account)
更改为意图来使用Intent
:
Now for the updateUI
method, this method should be defined by you. Basically, this is for your app to change what is shown to the user when he/she has signed into the app. If you want to open new activity when signedIn()
you can use Intent
by changing the updateUI(account)
in the activity result and onstart event to an intent:
startActivity(new Intent(MainActivity.this, SecondActivity.class));
并获取在SecondActivity
中登录的帐户:
And get which account is signedin in the SecondActivity
:
GoogleSignInAccount account = GoogleSignIn.g etLastSignedInAccount(this); //use this in onCreate
这篇关于适用于Android的Google登录:无法解析RC_SIGN_IN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!