1

我在 AppGallery 中有一个应用程序也可用于旧款华为手机,例如 P8 或 P9。我注意到旧手机有时会因为 HMS Core 过时而崩溃。我读到 HMS Core 可以自动更新,但我想在运行应用程序后立即更新。

使用地图时可以调用它(我在第二个屏幕上有它),但我想尽快通知用户有关过时的 HMS Core。

是否有任何示例如何手动调用 HMS Core 版本检查?

4

2 回答 2

2

华为手机手动升级HMS Core(APK),调用下方示例代码查看版本号是否早于5.0.0.300。如果是这样,就会出现 AppGallery 上的 HMS Core 下载屏幕。

此外,请记住替换URI 中的channelId。根据需要定义channelId以识别通道数据。channelId是一级频道的ID,即用户来自的频道。

public static void checkUpdate(Context context){
    String version = "5.0.0.300";
    String pkg = "com.huawei.hwid";
    if (version.compareTo(getVersionCode(pkg, context))  > 0) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("hiapplink://com.huawei.appmarket?appId=C10132067&channelId=xxxxx&referrer=&detailType=0"));
        context.startActivity(intent);
    }
}

private static String getVersionCode(String packageName, Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {
        PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
        return packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}
于 2020-09-18T06:00:11.993 回答
1

对的,这是可能的。这是提供的解决方案。一切都基于变量baseVersion。如果它的值高于您的 HMS Core 版本代码,那么您将得到提及的对话框。

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.util.Log;

import com.huawei.hms.adapter.AvailableAdapter;
import com.huawei.hms.adapter.internal.AvailableCode;
import com.huawei.hms.api.ConnectionResult;


/**
 * Check for HMS Update
 */
public class HmsUpdateUtil {
    private static final String TAG = "HmsUpdateUtil";

    private static boolean isInitialized = false;

    private static int versionCheckResult = 12;


    /**
     * Check if HMS needs update
     *
     * @param context context
     * @return result,0 Available, 1 not Available
     */
    public static int isHmsAvailable(Context context) {
        if (versionCheckResult == ConnectionResult.SUCCESS ) {
            return ConnectionResult.SUCCESS;
        }
        Log.d(TAG, "isInitialized is:" + isInitialized);

        if (isInitialized) {
            return 1;
        }

        // minimum HMS version, if less than this version, result will not be 0
        int baseVersion = 50000300;

        AvailableAdapter availableAdapter = new AvailableAdapter(baseVersion);
        int result = availableAdapter.isHuaweiMobileServicesAvailable(context);
        Log.i(TAG, "HMS update result is: " + result);
        
        isInitialized = true;
        
        if (result == ConnectionResult.SUCCESS) {
            Log.i(TAG, "HMS is avaiable");
        } else {
            if (availableAdapter.isUserResolvableError(result)) {
                resolution(availableAdapter, context);
            } else {
                Log.e(TAG, "HMS is not avaiable " + AvailableCode.ERROR_NO_ACTIVITY);
            }
        }
        versionCheckResult = result;
        return result;
    }

    private static void resolution(AvailableAdapter availableAdapter, Context context) {
        Log.i(TAG, "HMS update start :");
        Activity activity = findActivity(context);
        if (activity == null) {
            Log.e(TAG, "HMS is not available" + AvailableCode.ERROR_NO_ACTIVITY);
            return;
        }

        // this method will be call upgrade dialog box.
        availableAdapter.startResolution(activity, new AvailableAdapter.AvailableCallBack() {
            @Override
            public void onComplete(int result) {
                if (result == AvailableCode.SUCCESS) {
                    versionCheckResult = result;
                    Log.i(TAG, "HMS update start success");
                } else {
                    Log.e(TAG, "HMS update failed: " + result);
                    isInitialized = false;
                }
            }
        });
    }

    /**
     * Get Activity by Context
     * @param context context
     * @return Activity
     */
    public static Activity findActivity(Context context) {
        Activity activity = null;
        if (context instanceof Activity) {
            return (Activity) context;
        }
        if (context instanceof ContextWrapper) {
            ContextWrapper wrapper = (ContextWrapper) context;
            return findActivity(wrapper.getBaseContext());
        } else {
            return activity;
        }
    }
}
于 2020-09-17T10:20:53.120 回答