[英]How to check the whether the android device has google play services running on 10.2 or greater
根據谷歌官方頁面上的文檔,SMSRetriever api需要設備上的最小10.2的谷歌播放服務。
先決條件
SMS Retriever API僅適用於Play服務版本為10.2及更高版本的Android設備。 https://developers.google.com/identity/sms-retriever/request#2_start_the_sms_retriever
如何實施檢查? 我也檢查過這個方法
public int isGooglePlayServicesAvailable (Context context, int minApkVersion)
GoogleApiAvailability類。 但現在再次說明,在谷歌播放服務10.2的情況下,minApkVersion將具有什么價值。 任何人請告訴我如何以最佳方式實現這一點。
SmsRetrieverClient
遵循較新的GoogleApi
連接模式,因此您不必擔心版本兼容性,因為底層框架會為您處理所有分辨率。
實際上,通過向這些返回任務的API調用添加偵聽器來專門處理故障情況可能是更好的UX( https://developers.google.com/android/reference/com/google/android/gms/tasks/Task而不是事先檢查可用性。
只是努力完成版本比較。
public boolean onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (googlePlayServiceEnabled() && isWithSMSRetrieverSupportedPlayServiceVersion()){
//Good to go
return true;
}
else{
return false;
}
}
檢查GooglePlay服務是否已啟用,並在錯誤時顯示正確的錯誤對話框。
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;
}
隱式獲取已安裝的播放服務版本並將其與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;
}
我知道這是一個老問題,已經回答了。 但我需要寫這個記錄。
情況:
在我的情況下,galaxy S2(谷歌播放服務版本3.1.59),我無法從addOnSuccessListener
和addOnFailureListener
得到響應。 此外,未調用addOnCompleteListener
或addOnCanceledListener
。
我嘗試了什么:
我想使用isGooglePlayServicesAvailable
與minApkVersion
,但沒有谷歌播放服務的版本歷史列表!
我的解決方案是
使用isGooglePlayServicesAvailable
而不使用minApkVersion
。 我是指棄用的方法。 在該方法中,它正在使用GOOGLE_PLAY_SERVICES_VERSION_CODE
檢查Google Play版本,這是已安裝的Google Play服務。
@HideFirstParty
@KeepForSdk
public int isGooglePlayServicesAvailable(Context var1) {
return this.isGooglePlayServicesAvailable(var1, GOOGLE_PLAY_SERVICES_VERSION_CODE);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.