최신 Unity3D 버전을 사용하고 있습니다. LocationService.isEnabledByUser을 사용할 때 GPS가 활성화 또는 비활성화되었는지 여부를 알려야합니다. 그러나 항상 true를 반환합니다. Android 4.2 스마트 폰을 사용하고 있습니다.위치 서비스가 항상 활성화되어 있다고 말합니다.
이 문제의 원인은 무엇이며 어떻게 든 해결할 수 있습니까?
최신 Unity3D 버전을 사용하고 있습니다. LocationService.isEnabledByUser을 사용할 때 GPS가 활성화 또는 비활성화되었는지 여부를 알려야합니다. 그러나 항상 true를 반환합니다. Android 4.2 스마트 폰을 사용하고 있습니다.위치 서비스가 항상 활성화되어 있다고 말합니다.
이 문제의 원인은 무엇이며 어떻게 든 해결할 수 있습니까?
일부 기기에서는 LocationService.isEnabledByUser
에 문제가 있으며 앱에서 사용하지 않을 것입니다. 그것은 신뢰할 수 없습니다. 이것에 대한 자바 플러그인을 빌드하십시오. 나는 오래 전에 만든 것을 나눌 것이다.
자바 :
이 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();
으로 위치 설정을을 열어 위치 서비스을 가능하게 알릴 수 있습니다.
매니페스트'
나는 지금 어떤 버전의 안드로이드를 사용하고 있습니까? – Lundira
4.2를 사용합니다. 중요하지 않습니다. 그것은 어떤 안드로이드 버전에서 작동해야합니다. 나는 버전 6에서 그것을 시도하지 않았지만 효과가있다. 매니페스트에' '권한을 포함시켜야합니다. –
Programmer