【问题标题】:How to manually update the HMS Core?如何手动更新 HMS Core?
【发布时间】:2020-09-17 09:30:35
【问题描述】:

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

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

有没有手动调用HMS Core版本检查的示例?

【问题讨论】:

    标签: huawei-mobile-services huawei-developers


    【解决方案1】:

    在华为手机上手动升级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 "";
        }
    }
    

    【讨论】:

      【解决方案2】:

      是的,这是可能的。这是提供的解决方案。一切都基于变量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;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-23
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多