定位服务总是说;s已启用,即使它';s不是

本文关键字:不是 服务 定位 启用 | 更新日期: 2023-09-27 17:58:31

我使用的是最新的Unity3D版本。当使用LocationService.isEnabledByUser时,应该告诉我GPS是启用还是禁用。然而,它总是返回真的。我使用的是安卓4.2智能手机。

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

定位服务总是说;s已启用,即使它';s不是

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