2013-03-26 8 views
1

Criteria.ACCURACY_COARSE를 사용하여 위치를 얻지 만 정확하지 않습니다. GPS Criteria.ACCURACY_FINE을 사용하려고했지만 onLocationChanged(Location location) 메서드가 호출되지 않았습니다.
몇 가지 조사를했지만 왜 그럴 수 없었습니다.
StackOverflow에 비슷한 질문이 많이 있지만 불행히도 내 문제를 해결할 수있는 항목을 찾을 수 없습니다.GPS로부터 현재 위치 얻기

private void locationInformationSettings(){ 
    Criteria criteria= new Criteria();; 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); //using Criteria.ACCURACY_COARSE works 
    //criteria.setPowerRequirement(Criteria.POWER_LOW); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    String providername = locationManager.getBestProvider(criteria, true); 

    myLocationListener = new MyLocationListener(); 

    locationManager.requestLocationUpdates(
      providername, //LocationManager.GPS_PROVIDER, 
      Utils.MINIMUM_TIME_BETWEEN_UPDATES, 
      Utils.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
      myLocationListener 
    );  
} 

private class MyLocationListener implements LocationListener { 

    public void onLocationChanged(Location location) { 

     locLongitude = location.getLongitude(); 
     locLatitude = location.getLatitude(); 
     String message = String.format(
       "New Location \n Longitude: %1$s \n Latitude: %2$s", 
       locLongitude, locLatitude); 
     Toast.makeText(RecordActivity.this, message, Toast.LENGTH_LONG).show(); 
     Log.e(TAG, "Location information received:(" + locLongitude + "," + locLatitude +")"); 
     locationManager.removeUpdates(myLocationListener); 
    } 

    public void onStatusChanged(String s, int i, Bundle b) { 
     Toast.makeText(RecordActivity.this, "Provider status changed", 
       Toast.LENGTH_LONG).show(); 
    } 

    public void onProviderDisabled(String s) { 
     Toast.makeText(RecordActivity.this, 
       "Provider disabled by the user. GPS turned off", 
       Toast.LENGTH_LONG).show(); 
    } 

    public void onProviderEnabled(String s) { 
     Toast.makeText(RecordActivity.this, 
       "Provider enabled by the user. GPS turned on", 
       Toast.LENGTH_LONG).show(); 
    } 

} 

매니페스트 파일 권한 :

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

사용하여 네트워크 공급자 작품과 GPS 공급자가 작동하지 않는 이유는
다음은 내 소스 코드?
장치 설정에서 GPS 서비스를 확인하고 Android 4.3 및 Android 2.3.3에서 테스트했습니다.
미리 감사드립니다.

+0

대신 기준 대신 LocationManager.GPS_PROVIDER를 사용하여 위치 업데이트를 요청한 다음 먼저 작동하는지 확인하십시오. –

+0

@HoanNguyen 주석에서 알 수 있듯이 시도했지만 작동하지 않았습니다. –

+0

GPS 위치를 획득하는 것이 즉각적인 것은 아닙니다. 상단 트레이에서 수정 프로그램을 가져 오려고 GPS 아이콘이 표시되어야합니다. –

답변

2

당신이 갈

package com.test.locationmanager; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.location.LocationProvider; 
import android.os.Bundle; 
import android.widget.TextView; 

public class LocationManagerStatus extends Activity { 

private LocationManager locationManager; 
private TextView textView; 
private final LocationListener gpsLocationListener = new LocationListener() { 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     final String tvTxt = textView.getText().toString(); 
     switch (status) { 
     case LocationProvider.AVAILABLE: 
      textView.setText(tvTxt + "GPS available again\n"); 
      break; 
     case LocationProvider.OUT_OF_SERVICE: 
      textView.setText(tvTxt + "GPS out of service\n"); 
      break; 
     case LocationProvider.TEMPORARILY_UNAVAILABLE: 
      textView.setText(tvTxt + "GPS temporarily unavailable\n"); 
      break; 
     } 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     textView.setText(textView.getText().toString() 
       + "GPS Provider Enabled\n"); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     textView.setText(textView.getText().toString() 
       + "GPS Provider Disabled\n"); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     locationManager.removeUpdates(networkLocationListener); 
     textView.setText(textView.getText().toString() 
       + "New GPS location: " 
       + String.format("%9.6f", location.getLatitude()) + ", " 
       + String.format("%9.6f", location.getLongitude()) + "\n"); 
    } 
}; 
private final LocationListener networkLocationListener = new LocationListener() { 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     final String tvTxt = textView.getText().toString(); 
     switch (status) { 
     case LocationProvider.AVAILABLE: 
      textView.setText(tvTxt + "Network location available again\n"); 
      break; 
     case LocationProvider.OUT_OF_SERVICE: 
      textView.setText(tvTxt + "Network location out of service\n"); 
      break; 
     case LocationProvider.TEMPORARILY_UNAVAILABLE: 
      textView.setText(tvTxt 
        + "Network location temporarily unavailable\n"); 
      break; 
     } 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     textView.setText(textView.getText().toString() 
       + "Network Provider Enabled\n"); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     textView.setText(textView.getText().toString() 
       + "Network Provider Disabled\n"); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     textView.setText(textView.getText().toString() 
       + "New network location: " 
       + String.format("%9.6f", location.getLatitude()) + ", " 
       + String.format("%9.6f", location.getLongitude()) + "\n"); 
    } 
}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    textView = (TextView) findViewById(R.id.textview); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 5000, 0, 
      networkLocationListener); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
      3000, 0, gpsLocationListener); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    locationManager.removeUpdates(networkLocationListener); 
    locationManager.removeUpdates(gpsLocationListener); 
} 
} 
0

답변없이 거의 2 개월이 지난 후에도 나는 다음과 같이 작업하기로 결정했습니다.
다른 사람들이 시간을 절약하는 데 도움이되기를 바랍니다.

public class GpsLocationService extends Service implements LocationListener{ 

    private final String TAG = getClass().getSimpleName(); 

    private LocationManager locationManager; 
    private String provider; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.i(TAG, "onCreate()"); 
     // Get the location manager 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     // Define the criteria how to select the locatioin provider -> use 
     // default 
     Criteria criteria = new Criteria(); 
     provider = locationManager.getBestProvider(criteria, false); 
     Location location = locationManager.getLastKnownLocation(provider); 

     // Initialize the location fields 
     if (location != null) { 
      Log.d(TAG, "Provider " + provider + " has been selected."); 
      onLocationChanged(location); 
     } 
     else { 
      Log.d(TAG, "Location not available!"); 
     } 

    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.i(TAG, "GpsLocationService has started!"); 


     locationManager.requestLocationUpdates(provider, 400, 1, this); 

     return START_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     Log.i(TAG, "GpsLocationService is destroyed!"); 
     super.onDestroy(); 
     locationManager.removeUpdates(this); 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 



    @Override 
    public void onLocationChanged(Location location) { 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     Log.i(TAG, "onLocationChanged() lat = " + lat + " long = " + lng);  
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

} 

그리고 매니페스트 파일에

다음과 같은 권한을 추가하는 것을 잊지 마세요 :

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 

또한 서비스를 언급 :

여기
<service android:name=".GpsLocationService"></service>