I am trying to make use of the Google Api Java API in my Unity app(for the Android build only) I want to use the DetectedActivity ability which can detect if the phone is in car, the user is running, walking or standing still.
(Kind of odd that you need to connect[to your google play account?] in order to use this specific function which probably all done locally?)
I was able to create Java objects with the AndroidJavaObject in C# scripts for another purpose(reading GPS from the Android API since the Unity one is bugged as of Unity 5.5). However, I cannot seem to create the(abstract) class of GoogleApiClient, or the Builder as an object. The code I tried to create the Builder was:
public void ActivateActivityDetector()
{
#if UNITY_ANDROID
using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
if (debugText!=null)
debugText.text = "Ok1";
try
{
AndroidJavaObject api = new AndroidJavaObject("com.google.android.gms.common.api.GoogleApiClient$Builder", activityContext);
if (debugText!=null)
debugText.text = (api!=null)?"Yes":"No";
}
catch (System.Exception e)
{
if (debugText!=null)
debugText.text = e.ToString();
}
}
#endif
}
However, I keep getting an exception that the class is not found for any sort of variation I try. I suspect this is perhaps the SDK Unity use to build the apk is the "vanilla" Android SDK, while there is a different SDK that also includes the Google API?
In any case, I would like to know if it is possible to use the Google API service, specifically the DetectedActivity, under Unity? Another possibility is to take the code directly from the Google API source code which I believe is available as open source?(Though I am not sure about the licensing and if I can use it in a commercial project).
The Google API is indeed not part of the Android SDK. To use it you need to set up Google Play Services in your Android project.
When you go to your build settings in unity, there should be an option named "Google Android Project" (like here). When you select that, Unity will create an Android project folder instead of an APK. In there, you should be able to setup the play services like described in the first link.
As far as I know, you'll have to build your APK using that project and Android Studio from that point on though. So you might want to implement some dummies for development purposes.