【问题标题】:How to detect huawei device models from other android models in flutter?如何在flutter中从其他android型号中检测华为设备型号?
【发布时间】:2021-11-09 11:44:04
【问题描述】:
由于我的应用是跨平台的,所以没有遇到什么问题,但是到了android,出现了一些问题,比如华为移动设备无法访问google play,并且有自己的应用商店(App Gallery),问题出在我的应用程序的规格之一是强制用户下载最新版本,我不知道如何检测华为设备让用户下载我的应用程序库中的应用程序。
【问题讨论】:
标签:
android
flutter
mobile
huawei-mobile-services
huawei-developers
【解决方案1】:
您可以获取设备制造商来检测华为设备
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.manufacturer}');
使用的插件 - https://pub.dev/packages/device_info
或者
您可以在设备中查看GooglePlayServices的可用性
GooglePlayServicesAvailability availability = await GoogleApiAvailability.instance.checkGooglePlayServicesAvailability();
需要插件 - https://pub.dev/packages/google_api_availability
注意:如果使用GooglePlayServices检查,还需要添加平台检查,因为它在iOS设备的情况下返回false。
【解决方案2】:
您可以按照Docs通过检查设备是否安装了HMS Core APK来检测华为设备。
// 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();
【解决方案3】:
判断设备是否有HMS的方法如下:
HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context)
实施检查HMS是否可用。
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);
}
}