如何在flutter中检测huawei设备型号与其他android机型?

rvpgvaaj  于 2023-09-28  发布在  Android
关注(0)|答案(3)|浏览(337)

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

nbewdwxp

nbewdwxp1#

您可以按照此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();
v09wglhw

v09wglhw2#

**更新:**device_info插件现已停用。

使用https://pub.dev/packages/device_info_plus,它提供了类似的功能。
您可以找设备制造商来检测华为设备

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。

eimct9ow

eimct9ow3#

判断设备中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);
    }
}

相关问题