Android 我有很多关于Firebase身份验证的红线
我在我的应用程序项目中添加了Firebase,并试图在我的应用程序中添加身份验证,但似乎效果不好 我有很多红线,我不知道发生了什么样的问题。你能帮我解决这个问题吗?Android 我有很多关于Firebase身份验证的红线,android,firebase,firebase-authentication,Android,Firebase,Firebase Authentication,我在我的应用程序项目中添加了Firebase,并试图在我的应用程序中添加身份验证,但似乎效果不好 我有很多红线,我不知道发生了什么样的问题。你能帮我解决这个问题吗? 谢谢。这是解决方案,将此添加到build.gradle(模块应用程序)中 问题解决。这是我的代码 apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig {
谢谢。这是解决方案,将此添加到build.gradle(模块应用程序)中 问题解决。这是我的代码
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.kse.jackieapp"
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-core:10.0.1'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
我认为您没有在项目中添加BaseActivity。 遵循以下步骤
如果有问题,请告诉我。请查看全明星标志
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
**GoogleSignInResult** result = **Auth**.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.**isSuccess**()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.**getSignInAccount**();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.**activity_google**);
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
// [START_EXCLUDE silent]
showProgressDialog();
// [END_EXCLUDE]
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(**GoogleSignInActivity**.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// [START_EXCLUDE]
hideProgressDialog();
// [END_EXCLUDE]
}
});
}
private void updateUI(FirebaseUser user) {
hideProgressDialog();
if (user != null) {
mStatusTextView.setText(getString(R.string.**google_status_fmt**, user.getEmail()));
mDetailTextView.setText(getString(R.string.**firebase_status_fmt**, user.getUid()));
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
} else {
mStatusTextView.setText(R.string.**signed_out**);
mDetailTextView.setText(null);
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);
}
}
@覆盖
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
//从GoogleSignInApi.getsigninent(…)启动Intent返回的结果;
if(requestCode==RC\u登录){
**GoogleSignInResult**result=**Auth**.GoogleSignInApi.getSignInResultFromIntent(数据);
如果(结果。**isSuccess**()){
//Google登录成功,通过Firebase验证
GoogleSignInAccount账户=结果。**getSignInAccount**();
firebaseAuthWithGoogle(账户);
}否则{
//Google登录失败,请适当更新UI
//[开始时不包括]
updateUI(null);
//[完]
}
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.*activity_google**);
私有void firebaseAuthWithGoogle(谷歌签名帐户){
Log.d(标记为“firebaseAuthWithGoogle:+acct.getId());
//[启动\u排除静默]
showProgressDialog();
//[完]
AuthCredential credential=GoogleAuthProvider.getCredential(acct.getIdToken(),null);
mAuth.SIGNWITH凭证(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
Log.d(标记“signInWithCredential:onComplete:+task.isSuccessful()”);
//如果登录失败,则向用户显示消息。如果登录成功
//将通知身份验证状态侦听器,并使用逻辑来处理
//可以在侦听器中处理已登录用户。
如果(!task.issusccessful()){
w(标记“signInWithCredential”,task.getException());
Toast.makeText(**GoogleSignenActivity**。这是“身份验证失败”,
吐司。长度(短)。show();
}
//[开始时不包括]
hideProgressDialog();
//[完]
}
});
}
私有void updateUI(FirebaseUser){
hideProgressDialog();
如果(用户!=null){
mStatusTextView.setText(getString(R.string.*google_status_fmt**,user.getEmail());
mDetailTextView.setText(getString(R.string.*firebase_status_fmt**,user.getUid());
findViewById(R.id.sign_in_按钮)。设置可见性(View.GONE);
findViewById(R.id.sign\u out\u和\u disconnect).setVisibility(View.VISIBLE);
}否则{
mStatusTextView.setText(R.string.*signed_out**);
mDetailTextView.setText(null);
findViewById(R.id.sign_in_按钮)。设置可见性(View.VISIBLE);
findViewById(R.id.sign\u out\u和\u disconnect).setVisibility(View.GONE);
}
}
共享您的代码和生成。gradle(模块应用程序)您必须将BaseActivity添加到代码中,或者必须将BaseActivity替换为AppCompactActivity我添加了BaseActivity,但遇到了更多问题。我将上载无法编译我的BaseActivity中的“加载”屏幕截图。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
**GoogleSignInResult** result = **Auth**.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.**isSuccess**()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.**getSignInAccount**();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.**activity_google**);
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
// [START_EXCLUDE silent]
showProgressDialog();
// [END_EXCLUDE]
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(**GoogleSignInActivity**.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// [START_EXCLUDE]
hideProgressDialog();
// [END_EXCLUDE]
}
});
}
private void updateUI(FirebaseUser user) {
hideProgressDialog();
if (user != null) {
mStatusTextView.setText(getString(R.string.**google_status_fmt**, user.getEmail()));
mDetailTextView.setText(getString(R.string.**firebase_status_fmt**, user.getUid()));
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
} else {
mStatusTextView.setText(R.string.**signed_out**);
mDetailTextView.setText(null);
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);
}
}