Resolved How to call a Java plugin with context

Minapps

Member
Joined
Aug 13, 2019
Messages
7
Programming Experience
Beginner
Hi, my game needs to know if the GPS is activated on an android device but I get an error. I made a plugin in Android Studio that returns true if GPS is activated and false if it isn't :

Java:
package com.minapps.minappsandroidlibrary;
import android.location.LocationManager;

public class AndroidClass {

    Context context;

    public boolean isLocationEnabled(Context ctx) {
            Log.d("UnityGPS", "Check GPS : ");

            this.context = ctx;

            LocationManager locationManager = (LocationManager) this.context.getSystemService(LOCATION_SERVICE);

            boolean state;
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                state = true;
            }else if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                state = false;
            }else{
                state = false;
            }
           return state;
        }
}

and in Unity I call it like that :

C#:
// Check GPS
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.minapps.minappsandroidlibrary.AndroidClass");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
gpsState = androidPlugin.CallStatic<bool>("isLocationEnabled", context);
if (gpsState == true)
{
    CheckText.text = "GPS Enabled";
}
else if (gpsState == false)
{
    CheckText.text = "GPS Disabled";
}

My error :

Java:
 (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
2019-08-14 15:44:08.629 23303-23336/? E/Unity: AndroidJavaException: java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "currentActivity" in class "Lcom/minapps/minappsandroidlibrary/AndroidClass;" or its superclasses
    java.lang.NoSuchFieldError: no "Ljava/lang/Object;" field "currentActivity" in class "Lcom/minapps/minappsandroidlibrary/AndroidClass;" or its superclasses
        at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
        at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
        at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:88)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:216)
        at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
      at UnityEngine.AndroidJNISafe.CheckException () [0x00091] in <5aceb54cdbcc492fbae9ab05aab81814>:0
      at UnityEngine.AndroidJNISafe.GetStaticFieldID (System.IntPtr clazz, System.String name, System.String sig) [0x00011] in <5aceb54cdbcc492fbae9ab05aab81814>:0
      at UnityEngine._AndroidJNIHelper.GetFieldID (System.IntPtr jclass, System.

I think it comes from the context, I don't know how to pass the unity context to the java file correctly.
 
Last edited by a moderator:
Oh i'm so sorry, like exactly at the same time I posted this my build finished and It worked !
my error came from this :

AndroidJavaClass unityPlayer = new AndroidJavaClass("com.minapps.minappsandroidlibrary.AndroidClass");

and needed to be change to this :

AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

and the java file needed to be this :

Java:
public static boolean isLocationEnabled(Context ctx) {
        Log.d("UnityGPS", "Check GPS : ");

        LocationManager locationManager = (LocationManager) ctx.getSystemService(LOCATION_SERVICE);

        boolean state;
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            state = true;
        }else if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            state = false;
        }else{
            state = false;
        }
       return state;
    }
 
Back
Top Bottom