2009-06-26 6 views
27

현재 Android에서 GPS와 호환되는 앱을 작성 중입니다. 지금은 GPS가 활성화되었는지 여부를 알아낼 수 있습니다. 내 문제는 응용 프로그램 시작시 GPS를 사용하지 않도록 설정하려는 경우입니다. 어떻게 프로그래밍 방식으로이 작업을 수행 할 수 있습니까?Android Cupcake에서 GPS를 프로그래밍 방식으로 사용하는 방법

+0

이 컵 케이크의 버전에 따라 다릅니다 꺼져있는 경우하지

public boolean isLocationServiceEnabled(){ LocationManager locationManager = null; boolean gps_enabled= false,network_enabled = false; if(locationManager ==null) locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); try{ gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); }catch(Exception ex){ //do nothing... } try{ network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); }catch(Exception ex){ //do nothing... } return gps_enabled || network_enabled; } 

그런 다음 마지막으로 엽니 다. 1.5 doesnt는 그것을 명백하게 허용한다. – D3vtr0n

답변

51

Android 1.5부터 시작할 수 없습니다. 당신이 할 수있는 대부분의 일은 팝업으로 활동을 열어 사용자가 그것을 켜고 끌 수있게 해줍니다. 이 활동을 열기 위해 목적을 세우는 데 android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS에있는 활동을 사용하십시오.

+3

왜 비활성화 되었습니까? 왜 개발자가 이것을 토글하지 못하게합니까? Power Control 위젯은 할 수 있어야합니다. 너 생각 안해? –

+24

개인 정보 보호를 이유로 사용 중지되었습니다. 사용자가 GPS를 끄기를 원한다면 사용자는 GPS를 꺼야합니다. – CommonsWare

+0

이 사람들은 그것을 알아 낸 것 같습니다. 불행히도 APK는 난독 화되었고 어떻게 완성되었는지 알 수 없었습니다. URL : https : //market.android.com/details? id = at.abraxas.powerwidget.free & hl = ko –

15
if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) 
{ 
    Intent myIntent = new Intent(Settings.ACTION_SECURITY_SETTINGS); 
    startActivity(myIntent); 
} 
+8

.isProviderEnabled는 LocationManager의 정적 메소드가 아니므로이 코드는 android 2.2에서 컴파일되지 않았습니다.나를위한 작업 코드는 다음과 같습니다 (서식에 사과) LocationManager locationManager = (위치 관리자) getSystemService (LOCATION_SERVICE); if (! locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER)) { 의도 myIntent = 새로운 의도 (Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity (myIntent); } –

-5

귀하의 질문에 이러한 속성에있는 안드로이드의 사용자 수준에있는 경우 : "Settings -> Location -> Use wireless networks" -> "Settings -> Location -> Use GPS satellites".

그러나 개발자는 적절한 사용 권한을 가지고 클래스 "android.provider.Settings.Secure"을 사용할 수 있습니다.

+0

이것은 답변이 아닙니다. 질문을 한 사람은 다음을 원합니다. –

3

는 다음을 사용할 수 있습니다 :

try { 
    Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true); 
} catch (Exception e) { 
    logger.log(Log.ERROR, e, e.getMessage()); 
} 

하지만 시스템 서명 보호 수준이있는 경우에만 작동합니다. 그래서 당신은 실제로 그것을 사용하는 당신의 자신의 이미지를 요리해야합니다 당신이

private void turnGPSOnOff(){ 
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
    if(!provider.contains("gps")){ 
    final Intent poke = new Intent(); 
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
    poke.setData(Uri.parse("3")); 
    sendBroadcast(poke); 
    //Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show(); 
    } 
} 
6

이 방법은 코드가 도움이 될 수 있습니다 클릭 한 번으로 필요한 경우).

+2

예 ** 최대 2.2 (sdk 8) **까지 가능합니다. 자세한 내용은 [프로그래밍 방식으로 GPS를 작업자와 함께 사용] (http://stackoverflow.com/a/5305835/383414)을 참조하십시오. –

0

당신은 (위치 서비스를 사용하도록 사용자에게 묻습니다 플레이 서비스의 Location Settings Dialog를 사용해야합니다 위해 /이

1

먼저 위치 서비스가 켜져 있는지 확인하십시오.

확인 위치 서비스를 사용하도록 설정 또는 위치 서비스가 이전에

if (isLocationServiceEnabled())) { 
      //DO what you need... 
    } else { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Seems Like location service is off, Enable this to show map") 
     .setPositiveButton("YES", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
          Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
            startActivity(intent); 
           } 
          }).setNegativeButton("NO THANKS", null).create().show(); 
       } 
관련 문제