How to do error handling for outdated Google Play Services avoiding application crash on android?

Asked

Viewed 682 times

0

I’m creating an app that uses Firebase, and which in turn uses the Google Play Services.

In my tests, when the Google Play Services is outdated, it crashes the application and gives an error saying the application has stopped.

Then appears a notification saying: Update Google Play Services - App only works with an updated version of Google Play Services.

It is possible to make an error treatment to warn the user that Google Play Services update is required without crashing the application and closing it?

He gives me the slip when I run this line:

 Button loginbtn = (Button) findViewById(R.id.loginbtn);
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               LoginProcess("[email protected] ", "senha123456");
            }
        });

...

public void LoginProcess(String email, String password){
    mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        FirebaseUser user = mAuth.getCurrentUser();
                        Toast.makeText(getParent(), "Logado com sucesso.", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getParent(), "Falha de Login.", Toast.LENGTH_SHORT).show();
                    }
                }
            });
}
  • Ask the question the code you use to "get" Google Play Services.

  • Thanks for answering @ramaral, I was able to solve it. The code that used google play services was already in question, which is the Firebaseuser firebase that in turn needs Google Play Services to work. I’ve added the answer with my solution.

1 answer

0


I managed to solve the problem as follows:

I found an outdated code on the net, after hours searching, and updating to the new method was like this inside a function I created:

public Boolean GServicesCheck() {
    int result = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);

    switch(result) {
        case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
            Toast.makeText(this, "O Google Play Services precisa ser atualizado para acessar esta função.", Toast.LENGTH_SHORT).show();
            Log.d("FVRLOG","SERVICE_VERSION_UPDATE_REQUIRED");
            return false;
        case ConnectionResult.SUCCESS:
            Log.d("FVRLOG", "Play service available success");
            return true;
        default:
            Log.d("FVRLOG", "unknown services result: " + result);
            Toast.makeText(this, "Houve um erro com o Google Play Services, entre em contato com o administrador.", Toast.LENGTH_SHORT).show();
            return false;
    }
}

And now I check every time I run something that uses Google Play Services this way:

       @Override
        public void onClick(View view) {
         if(GServicesCheck()) {
           LoginProcess("[email protected] ", "senha123456");
         }
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.