// это мое первое занятие //
public class MainActivity extends Main2Activity implements
GoogleApiClient.OnConnectionFailedListener,View.OnClickListener {
private FirebaseAuth.AuthStateListener mauthStateListener;
private GoogleApiClient googleApiClient;
public ProgressBar progressBar;
public static final int RC_SIGN_IN=9001;
public static final String TAG="GoogleActivity";
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=(ProgressBar)findViewById(R.id.progressBarnew);
mAuth = FirebaseAuth.getInstance();
findViewById(R.id.google_sign_in).setOnClickListener(this);
TextView textView= (TextView)findViewById(R.id.welcome);
GoogleSignInOptions gso=new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();
googleApiClient =new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();
mAuth=FirebaseAuth.getInstance();
mauthStateListener= new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user=firebaseAuth.getCurrentUser();
if(user!=null)
{
Log.d(TAG,"onAuthStateChange:signed_in"+user.getUid());
}else
{
Log.d(TAG,"onAuthStateChange:signed_out");
}
}
};
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==RC_SIGN_IN)
{
GoogleSignInResult result=Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess())
{
// findViewById(R.id.progressBarnew).setVisibility(View.GONE);
GoogleSignInAccount account=result.getSignInAccount();
firebaseAuthwithgoogle(account);
}
}
}
private void firebaseAuthwithgoogle(GoogleSignInAccount acct)
{
Log.d(TAG,"firebaseAuthwithgoogle:"+acct.getId());
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,"signinwithcreditials:oncomplete"+task.isSuccessful());
FirebaseUser user=mAuth.getCurrentUser();
updateUI(user);
if (!task.isSuccessful())
{
Log.d(TAG,"signinwithcreditials"+task.getException());
Toast.makeText(MainActivity.this,"authentication failed please try again",Toast.LENGTH_SHORT).show();
findViewById(R.id.progressBarnew).setVisibility(View.GONE);
}
}
});
}
private void signIn()
{
findViewById(R.id.progressBarnew).setVisibility(View.VISIBLE);
Intent signInIntent=Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(signInIntent,RC_SIGN_IN);
}
@Override
protected void onStop() {
super.onStop();
if (mauthStateListener!=null)
{
mAuth.removeAuthStateListener(mauthStateListener);
}
}
@Override
protected void onStart() {
// mAuth.addAuthStateListener(mauthStateListener);
if (mAuth.getCurrentUser()!=null)
{
finish();
startActivity(new Intent(getApplicationContext(),Main2Activity.class));
}
super.onStart();
}
@Override
public void onClick(View v) {
signIn();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG,"onconnectionfailed"+connectionResult);
Toast.makeText(this,"google play services error",Toast.LENGTH_SHORT).show();
}
private void updateUI(FirebaseUser user) {
if (user != null) {
Toast.makeText(this,"Redirect To Home Page ",Toast.LENGTH_SHORT).show();
findViewById(R.id.progressBarnew).setVisibility(View.GONE);
Toast.makeText(MainActivity.this,"Login Successfull",Toast.LENGTH_SHORT).show();
finish();
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}
else
{
//findViewById(R.id.google_sign_in).setVisibility(View.VISIBLE);
}
}
@Override
public void onBackPressed() {
finish();
onStop();
}
}
// это второе занятие
public class Main2Activity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private FirebaseAuth.AuthStateListener mauthStateListener;
private GoogleSignInClient googleSignInClient;
FirebaseAuth auth;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
auth=FirebaseAuth.getInstance();
Button buttonout=(Button)findViewById(R.id.btnsignout);
buttonout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signout();
}
});
}
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START))
{
drawerLayout.closeDrawer(GravityCompat.START);
}
else
finish();
Toast.makeText(this,"Press again",Toast.LENGTH_SHORT).show();
}
private void updateUI(FirebaseUser user) {
if (user != null) {
Toast.makeText(this,"Redirect To Home Page ",Toast.LENGTH_SHORT).show();
findViewById(R.id.progressBarnew).setVisibility(View.GONE);
finish();
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}
else
{
}
}
public void signout()
{
GoogleSignInClient mGoogleSignInClient ;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(getBaseContext(), gso);
mGoogleSignInClient.signOut().addOnCompleteListener(Main2Activity.this,
new OnCompleteListener<Void>() { //signout Google
@Override
public void onComplete(@NonNull Task<Void> task) {
FirebaseAuth.getInstance().signOut(); //signout firebase
Intent setupIntent = new Intent(getBaseContext(),MainActivity.class);
Toast.makeText(getBaseContext(), "Logged Out", Toast.LENGTH_LONG).show(); //if u want to show some text
setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(setupIntent);
finish();
}
});
}
}
я хочуперейти от mainacctivity к main2activity после входа в систему с помощью кнопки входа в googlr.после нажатия на btn он показывает 2 раза: «К сожалению, MyApp остановился», после этого, когда я снова открываю приложение, оно работает нормально.
Я также проверил LogCat, это показывает эту ошибку
на Java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1113) в java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:588)
как я могу решить это?1019 *