定位服务总是说它已启用,即使它没有启用

时间:2016-07-23 16:57:15

标签: c# unity3d gps

我使用的是最新的Unity3D版本。使用LocationService.isEnabledByUser时,我应该被告知GPS是启用还是禁用。但是它总是返回true。我使用的是Android 4.2智能手机。

这个问题可能是什么原因,我可以以某种方式解决它吗?

1 个答案:

答案 0 :(得分:1)

某些设备上的LocationService.isEnabledByUser存在问题,我不相信在我的应用中使用它。这不可靠。只需为此构建一个java插件。我将分享我很久以前所做的那个。

<强>爪哇

创建一个名为LocationService的类。我们假设软件包名称为com.progammer.plugin,完整软件包名称为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#

创建一个名为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");
    }
}

在C#中使用插件

让我们制作一个简单的测试脚本来测试新的插件。这只会在Android设备上运行,所以不要指望它在编辑器中运行。

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();

    }
}

您甚至可以通过lsm.notifyUserToEnableLocationService();打开位置设置来通知播放器启用位置服务