如何在flutter中检测华为设备型号和其他android型号?



由于我的应用程序是跨平台的,没有遇到任何问题,但是当涉及到android时,出现了一些问题,例如华为移动设备不再能够访问google play,并且他们有自己的应用程序商店(App Gallery),问题来了,因为我的应用程序的规范之一是强制用户下载最新版本我不知道如何检测华为设备,让用户直接从应用库下载我的应用。

您可以参考本文档通过检查设备是否安装了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();

您可以获取设备制造商来检测华为设备

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。

判断HMS在设备中是否可用的方法如下:

HuaweiApiAvailability.getInstance () .isHuaweiMobileServicesAvailable(上下文)

检查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);
}
}

相关内容

  • 没有找到相关文章

最新更新