Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProviderInstaller doesn't work on emulators and caught error is not being read

Tags:

java

android

In my application, I am connecting to an SSL server and in order to make it possible for API's less than 21 I need to install ProviderInstaller. Below is a preview of my code:

if (Build.VERSION.SDK_INT < 21) {
     try {
      ProviderInstaller.installIfNeeded(this);

    } catch (Exception e) {

      //Crashes the app when accessing the getMessage
      Log.i("error", e.getMessage());
   }
 }

This works well on device with API 16, but it doesnt work on emulators with API 16. And when I try to access the getMessage() error it crashes the whole application.

Why doesn't the ProviderInstaller.installIfNeeded(this) work on emulators, and also why does the application crashes when accessing the e.getMessage() function?

like image 342
AndroidDev Avatar asked Oct 31 '18 16:10

AndroidDev


1 Answers

installIfNeeded can throw two exceptions GooglePlayServicesRepairableException and GooglePlayServicesNotAvailableException, They are thrown when Google Play Services is not up-to-date or enabled and when Google Play services are not available, respectively. Possibly there is a problem with Google Play services in the emulator.

And app crashes when you access getmessage() is because the exception that you are catching doesn't have a message. You need to log the entire exception, not just the exception's message. You can verify this from the documentation of getMessage which says

the detail message string of this Throwable instance (which may be null).

From documentation of GooglePlayServicesRepairableException

In these cases, client code can use getConnectionStatusCode() in conjunction with getErrorDialog(android.app.Activity, int, int) to provide users with a localized Dialog that will allow users to install, update, or otherwise enable Google Play services.

like image 82
a0x2 Avatar answered Nov 20 '22 04:11

a0x2