0

I am using the latest Unity3D version. When using LocationService.isEnabledByUser, I should be told whether the GPS is enabled or disabled. However it is always returns true. I am using an Android 4.2 smartphone.

What could be the cause of this issue, and can I somehow resolve it?

Bart
  • 19,692
  • 7
  • 68
  • 77
Lundira
  • 169
  • 3
  • 12
  • I will now, what version of android are you using? – Lundira Jul 29 '16 at 17:08
  • I use 4.2.That shouldn't matter. It should work on any Android version. I haven't tried it on version 6 but it should work. Make sure to include `` permission in the Manifest. – Programmer Jul 29 '16 at 17:22

1 Answers1

1

There is a problem with LocationService.isEnabledByUser on some devices and I wouldn't trust using that in my app. It is not reliable. Just build a java plugin for this. I will share the one I made long time ago.

Java:

Create a class called LocationService. Let's assume that package name is com.progammer.plugin and the full package name is com.progammer.plugin.LocationService.

import android.content.Context;
import android.content.Intent;

import android.location.LocationManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class LocationService {
    private static Context mContext;

    // Called From C# to get the Context Instance
    public static void receiveContextInstance(Context tempContext) {
        mContext = tempContext;
    }

    // http://stackoverflow.com/a/10311891/3785314
    public static boolean isLocationServiceEnabled() {
        LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        boolean gps_enabled = false;
        boolean network_enabled = false;

        try {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
            Log.e("CONTEXT", "Error: " + ex.getMessage());
        }

        try {
            network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
            Log.e("CONTEXT", "Error: " + ex.getMessage());
        }
        return (gps_enabled && network_enabled);
    }

    public static boolean isGPSLocationServiceEnabled() {
        LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        boolean gps_enabled = false;
        try {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
            Log.e("CONTEXT", "Error: " + ex.getMessage());
        }
        return gps_enabled;
    }

    public static boolean isNetworkLocationServiceEnabled() {
        LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        boolean network_enabled = false;
        try {
            network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
            Log.e("CONTEXT", "Error: " + ex.getMessage());
        }
        return network_enabled;
    }

    // http://stackoverflow.com/a/32797750/3785314
    @SuppressWarnings({ "deprecation" })
    public static boolean isAirplaneModeOn() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            /* API 17 and above */
            return Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
        } else {
            /* below */
            return Settings.System.getInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
        }
    }

    // http://stackoverflow.com/a/7713511/3785314
    public static void notifyUserToEnableLocationService() {
        CharSequence searchStr = "Please enable Location Service";
        Toast.makeText(mContext, searchStr, Toast.LENGTH_LONG).show();

        Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);

        gpsOptionsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(gpsOptionsIntent);
    }
}

C#:

Create a script called LocationServiceManager:

using UnityEngine;
using System.Collections;

public class LocationServiceManager
{

    AndroidJavaClass unityClass;
    AndroidJavaObject unityActivity;
    AndroidJavaObject unityContext;
    AndroidJavaClass customClass;

    public LocationServiceManager()
    {
        //Replace with your full package name
        sendContextReference("com.progammer.plugin.LocationService");
    }

    public void sendContextReference(string packageName)
    {
        unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
        unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");

        customClass = new AndroidJavaClass(packageName);
        customClass.CallStatic("receiveContextInstance", unityContext);
    }

    ///////////////////////////////////MAIN FUNCTIONS/////////////////////////////////////
    public bool isLocationServiceEnabled()
    {
        return customClass.CallStatic<bool>("isLocationServiceEnabled");
    }

    public bool isGPSLocationServiceEnabled()
    {
        return customClass.CallStatic<bool>("isGPSLocationServiceEnabled");
    }

    public bool isNetworkLocationServiceEnabled()
    {
        return customClass.CallStatic<bool>("isNetworkLocationServiceEnabled");
    }

    public bool isAirplaneModeOn()
    {
        return customClass.CallStatic<bool>("isAirplaneModeOn");
    }

    public void notifyUserToEnableLocationService()
    {
        customClass.CallStatic("notifyUserToEnableLocationService");
    }
}

To use the plugin in C#:

Let's make a simple Test Script to test the new plugin. This will only run on Android devices, so don't expect it to work in the Editor.

public class TestScript : MonoBehaviour
{
    public Text text;
    LocationServiceManager lsm;

    void Start()
    {
        lsm = new LocationServiceManager();

        text.text = "Air Plane Mode: " + lsm.isAirplaneModeOn();
        text.text += "\r\nLocation Service Enabled: " + lsm.isLocationServiceEnabled();
        text.text += "\r\nGPS Location Service Enabled: " + lsm.isGPSLocationServiceEnabled();
        text.text += "\r\nNetwork Location Service Enabled: " + lsm.isNetworkLocationServiceEnabled();

    }
}

You can even notify the player to enable Location Service by opening the Location Settings with lsm.notifyUserToEnableLocationService();.

Programmer
  • 121,791
  • 22
  • 236
  • 328