Reputation: 6611
As per the documentation on official page of google the SMSRetriever api needs google play services min 10.2 on device.
Prerequisites
The SMS Retriever API is available only on Android devices with Play services version 10.2 and newer. https://developers.google.com/identity/sms-retriever/request#2_start_the_sms_retriever
How to implement the check for that ? I have also checked the method
public int isGooglePlayServicesAvailable (Context context, int minApkVersion)
of GoogleApiAvailability class. But now again what will be value of minApkVersion in case of google play services 10.2. Any one please let me know how to implement this in a best way.
Upvotes: 3
Views: 1566
Reputation: 41
Just a work around to get the version comparision done.
public boolean onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (googlePlayServiceEnabled() && isWithSMSRetrieverSupportedPlayServiceVersion()){
//Good to go
return true;
}
else{
return false;
}
}
Checks if GooglePlay Service is Enabled and Displays proper error dialog on errors.
private boolean googlePlayServiceEnabled() {
try {
GoogleApiAvailability instance = GoogleApiAvailability.getInstance();
int responseCode = instance.isGooglePlayServicesAvailable(context);
switch (responseCode) {
case ConnectionResult.SUCCESS:
return true;
default:
Dialog errorDialog = instance.getErrorDialog(this, responseCode, 123);
errorDialog.setCancelable(false);
errorDialog.show();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Implicitly gets the installed play services version and compares it with 10.2
private boolean isWithSMSRetrieverSupportedPlayServiceVersion() {
try {
PackageInfo pi = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0);
if (pi == null)
return false;
if (pi.versionName == null || pi.versionName.isEmpty())
return false;
String playServicesVersion = pi.versionName;
return isPlayServicesVersionValidForSMSRetriever(playServicesVersion);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean isPlayServicesVersionValidForSMSRetriever(String v) {
try {
String index1 = v.substring(0, v.indexOf('.', 0));
int versionInt1 = Integer.parseInt(index1);
if (versionInt1 < 10)
return false;
else if (versionInt1 == 10) {
String index2 = v.substring(3, 4);
int versionInt2 = Integer.parseInt(index2);
if (versionInt2 < 2) {
return false;
} else {
return true;
}
} else
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Upvotes: 1
Reputation: 3497
I know this is an old question, and already answered. But I need to write this for the record.
Situation:
In my case, galaxy S2 (google play service version 3.1.59), I couldn' get response from neither addOnSuccessListener
nor addOnFailureListener
. Also, addOnCompleteListener
or addOnCanceledListener
was not called.
What I tried:
I want use isGooglePlayServicesAvailable
with minApkVersion
, but there was no version history list of google play service!
My solution is
Use isGooglePlayServicesAvailable
without minApkVersion
. I mean the deprecated method. In that method, it is checking google play version with GOOGLE_PLAY_SERVICES_VERSION_CODE
, and this is the google play service of installed one.
@HideFirstParty
@KeepForSdk
public int isGooglePlayServicesAvailable(Context var1) {
return this.isGooglePlayServicesAvailable(var1, GOOGLE_PLAY_SERVICES_VERSION_CODE);
}
Upvotes: 0
Reputation: 170
SmsRetrieverClient
follows the newer GoogleApi
connection pattern so you shouldn't have to worry about the version compatibility as the underlying framework handles all the resolution for you.
It may actually be better UX to specifically handle the failure case as well by adding listeners to these Task-returning API calls (https://developers.google.com/android/reference/com/google/android/gms/tasks/Task) instead of checking for availability beforehand.
Upvotes: 2