2012-01-07 4 views

답변

0

GPS가 비동기 적으로 작동하므로 별도의 스레드를 호출 할 필요가 없습니다. GPS는 위치를 가져올 때 사용자 인터페이스를 고정시키지 않습니다. 가장 좋은 방법은 progressDialog를 사용하고 position을 얻은 후에 progressDialog를 닫을 수 있다는 것입니다.

1

연구,

public class LocListener implements LocationListener { 
    private static double lat =0.0; 
    private static double lon = 0.0; 
    private static double alt = 0.0; 
    private static double speed = 0.0; 

    public static double getLat() 
    { 
     return lat; 
    } 

    public static double getLon() 
    { 
     return lon; 
    } 

    public static double getAlt() 
    { 
     return alt; 
    } 

    public static double getSpeed() 
    { 
     return speed; 
    } 

    @Override 
    public void onLocationChanged(Location location) 
    { 
     lat = location.getLatitude(); 
     lon = location.getLongitude(); 
     alt = location.getAltitude(); 
     speed = location.getSpeed(); 
    } 

    @Override 
    public void onProviderDisabled(String provider) {} 
    @Override 
    public void onProviderEnabled(String provider) {} 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
} 

지금 스레드에서 GPS를 시작하는이 코드를 사용합니다.

new Thread (new Runnable() 
{ 
    public void run() 
    { 
     locationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); 
      locationListener = new LocListener(); 
      locationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
    } 
}).start(); 
+0

동일한 프로 시저를 사용했지만 오류가 발생했습니다. Looper.prepare()를 호출하지 않은 스레드 내부에서 처리기를 만들 수 없습니다. – DevAndroid

+0

안녕 Arjun, 일부 코드 서식을 수정했습니다. 일부 공백을 제외하고는 아무 것도하지 않았습니다. 제대로 표시됩니다. –

+0

@DevAndroid 귀하의 질문에 Logcat 오류를 게시하십시오. –

0
private void turnGPSOn(){ 
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 

    if(!provider.contains("gps")){ //if gps is disabled 
     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); 
    } 
} 

private void turnGPSOff(){ 
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 

    if(provider.contains("gps")){ //if gps is enabled 
     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); 
    } 
} 
관련 문제