2016-07-23 9 views
0

Ich verwende die neueste Unity3D-Version. Bei Verwendung von LocationService.isEnabledByUser sollte mir gesagt werden, ob das GPS aktiviert oder deaktiviert ist. Es wird jedoch immer wahr zurückgegeben. Ich benutze ein Android 4.2 Smartphone.Location Service sagt immer, es ist aktiviert, auch wenn es nicht

Was könnte die Ursache für dieses Problem sein, und kann ich es irgendwie lösen?

+0

Ich werde jetzt, welche Version von Android verwenden Sie? – Lundira

+0

verwende ich 4.2. Das sollte nicht wichtig sein. Es sollte auf jeder Android-Version funktionieren. Ich habe es nicht auf Version 6 versucht, aber es sollte funktionieren. Stellen Sie sicher, dass Sie im Manifest die Berechtigung '' angeben. – Programmer

Antwort

1

Es gibt ein Problem mit LocationService.isEnabledByUser auf einigen Geräten und ich würde nicht vertrauen, dass in meiner App zu verwenden. Es ist nicht zuverlässig. Erstellen Sie einfach ein Java-Plugin dafür. Ich werde denjenigen teilen, den ich vor langer Zeit gemacht habe.

Java:

Erstellen Sie eine Klasse LocationService genannt. Angenommen, der Paketname lautet com.progammer.plugin und der vollständige Paketname lautet 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#:

ein Skript LocationServiceManager genannt anlegen:

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"); 
    } 
} 

Um das Plugin in C# zu verwenden:

Lassen Sie uns ein einfaches Testskript machen das testen neues Plugin Dies funktioniert nur auf Android-Geräten, also erwarte nicht, dass es im Editor funktioniert.

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

    } 
} 

Sie können sogar benachrichtigen der Spieler Location Service durch Öffnen der Standorteinstellung mit lsm.notifyUserToEnableLocationService(); zu ermöglichen.

+0

Sie müssen in die Manifest '' Berechtigung aufnehmen oder es wird abstürzen. – Programmer

Verwandte Themen