【问题标题】:Location service always says it's enabled, even when it's not定位服务总是说它已启用,即使它没有
【发布时间】:2016-07-23 16:57:15
【问题描述】:

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

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

【问题讨论】:

  • 我现在,你用的是什么版本的android?
  • 我用的是 4.2。没关系。它应该适用于任何 Android 版本。我还没有在版本 6 上尝试过,但它应该可以工作。确保在清单中包含<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 权限。

标签: c# unity3d gps


【解决方案1】:

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

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();打开位置设置来通知玩家启用位置服务

【讨论】:

  • 必须在 Manifest 中包含&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; 权限,否则会崩溃。
猜你喜欢
  • 2016-08-09
  • 2016-09-30
  • 2021-12-08
  • 2016-02-04
  • 2016-05-14
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 2011-08-14
相关资源
最近更新 更多