Java Question Detect Huawei Mobile Services

Multiverse app

Active Member
Licensed User
Longtime User
Following this thread, I can know how to detect if gms are installed.
In the same way, I want to know if hms (Huawei mobile services) are installed. According to this answer on stack overflow, both the methods are almost the same:


Java:
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;
}

public static boolean isGmsAvailable(Context context) {
    boolean isAvailable = false;
    if (null != context) {
        int result = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
        isAvailable = (com.google.android.gms.common.ConnectionResult.SUCCESS == result);
    }
    Log.i(TAG, "isGmsAvailable: " + isAvailable);
    return isAvailable;
}


I tried to add this in B4A:
B4X:
Sub CheckForHuaweiMobileServices As Boolean
   Dim HuaweiApiAvailablity As JavaObject
   HuaweiApiAvailablity = HuaweiApiAvailablity.InitializeStatic("HuaweiApiAvailability").RunMethod("getInstance", Null)
   Dim context As JavaObject
   context.InitializeContext
   If HuaweiApiAvailablity.RunMethod("isHuaweiMobileServicesAvailable", Array(context)) <> 0 Then
     Return False
   End If
   Return True
End Sub

Also tried the inline Java code:
B4X:
#If JAVA
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;
import android.content.Context;


public BA ba;
Context context = ba.context;

public boolean isHmsAvailable() {
    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;
}
#End If

They do not work.
I've also added
B4X:
#AdditionalJar: push-4.0.1.300.aar
in my project.

Any solution?
 

DonManfred

Expert
Licensed User
Longtime User
They do not work.
From SO:
AFAIK these classes (HuaweiApiAvailability/GoogleApiAvailability) are available if you implement any of the Huawei's kit/Google's lib.

So i looks like you are missing something. Maybe another aar must be included?
 

Multiverse app

Active Member
Licensed User
Longtime User
So I did implement Huawei Push, and do have all the required aars:

B4X:
#AdditionalJar: agconnect-core-1.0.1.300.aar
#AdditionalJar: base-4.0.1.300.aar
#AdditionalJar: opendevice-4.0.1.300.aar
#AdditionalJar: push-4.0.1.300.aar
#AdditionalJar: network-common-4.0.2.301.aar
#AdditionalJar: network-grs-4.0.2.301.aar
#AdditionalJar: tasks-1.3.1.302.aar

Not sure what else is needed
 

Multiverse app

Active Member
Licensed User
Longtime User
In the inline Java code, it returns error:
src\com\multiverse\jarvis\hms.java:108: error: cannot find symbol
int result = HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context);
^
symbol: variable HuaweiApiAvailability
location: class hms



#If JAVA import anywheresoftware.b4a.BA; import anywheresoftware.b4a.BA.*; import android.content.Context; public BA ba; Context context = ba.context; public boolean isHmsAvailable() { 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; } #End If
 
Top