// Create an HmsApiAvailability instance
HmsApiAvailability client = new HmsApiAvailability();
// 0: HMS Core (APK) is available.
// 1: No HMS Core (APK) is found on device.
// 2: HMS Core (APK) installed is out of date.
// 3: HMS Core (APK) installed on the device is unavailable.
// 9: HMS Core (APK) installed on the device is not the official version.
// 21: The device is too old to support HMS Core (APK).
int status = await client.isHMSAvailable();
public class HmsGmsUtil {
private static final String TAG = "HmsGmsUtil";
/**
* Whether the HMS service on the device is available.
*
* @param context android context
* @return true:HMS service is available; false:HMS service is not available;
*/
public static boolean isHmsAvailable(Context context) {
boolean isAvailable = false;
if (null != context) {
int result = HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context);
isAvailable = (com.huawei.hms.api.ConnectionResult.SUCCESS == result);
}
Log.i(TAG, "isHmsAvailable: " + isAvailable);
return isAvailable;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHmsSignBtn = findViewById(R.id.hms_sign_btn);
mHmsSignBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signInByHms();
}
mGmsSignBtn = findViewById(R.id.gms_sign_btn);
mGmsSignBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signInByGms();
}
});
boolean isHmsAvailable = HmsGmsUtil.isHmsAvailable(this);
Log.i(TAG, "isHmsAvailable: " + isHmsAvailable + ", isGmsAvailable: " + isGmsAvailable);
if (isHmsAvailable && !isGmsAvailable) {
// Only hms, Sign In by huawei account.
mHmsSignBtn.setVisibility(View.VISIBLE);
} else if (!isHmsAvailable && isGmsAvailable) {
// Only gms, Sign In by google account.
mGmsSignBtn.setVisibility(View.VISIBLE);
} else if (isHmsAvailable && isGmsAvailable) {
// both hsm and hms, decide by developer.
mGmsSignBtn.setVisibility(View.VISIBLE);
mHmsSignBtn.setVisibility(View.VISIBLE);
} else if (!isHmsAvailable && !isGmsAvailable) {
// neither hms and gms, decide by developer.
mHmsSignBtn.setVisibility(View.VISIBLE);
}
}
3条答案
按热度按时间nbewdwxp1#
您可以按照此Docs通过检查设备是否安装了HMS Core APK来检测华为设备。
v09wglhw2#
**更新:**device_info插件现已停用。
使用https://pub.dev/packages/device_info_plus,它提供了类似的功能。
您可以找设备制造商来检测华为设备
使用的插件-https://pub.dev/packages/device_info
或者
您可以检查设备中
GooglePlayServices
的可用性需要插件-https://pub.dev/packages/google_api_availability
**注意:**如果使用
GooglePlayServices
检查,还需要添加平台检查,因为iOS设备返回false。eimct9ow3#
判断设备中HMS是否可用的方法如下:
HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context)
检查HMS是否可用。