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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With