I have an application in AppGallery available also for the older Huawei phones such as P8 or P9. I noticed that the old phones sometimes catch crash because of outdated HMS Core. I read that HMS Core can update itself automatically but I want do it immediately after I run the app.
It is possible to call it when you use map (I have it on the second screen) but I want inform user about outdated HMS Core as soon as possible.
Is there any sample how to manually call HMS Core version check?
Yes, it is possible. Here is the provided solution. Everything is based on variable baseVersion
. If it's value is higher than your HMS Core version code then you will get mentioned dialog.
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;
}
}
}