신청서에 What is the simplest and most robust way to get the user's current location on Android?의 Fedor의 예를 사용 중입니다. 모든 것이 작동하지만 모든 제공 업체가 활성화되어있는 동안 GPS 위치를 표시하려면 앱이 필요합니다. 독립 실행 형 GPS 서비스가 장치에서 활성화되었을 때 GPS가 표시되지만 다른 위치 서비스가 켜지면 GPS가 아닌 네트워크 위치가 표시됩니다. 사용할 수있는 경우 GPS 위치를 표시하려면 앱이 필요합니다. 최신 명령문을 사용한다는 내용의 명령문을 제거한 후에도 사용 가능한 경우 여전히 네트워크 위치가 표시됩니다. 이것은 Android에서의 위치 서비스에 대한 나의 첫 경험이므로 어떤 도움도 크게 받으실 수 있습니다.네트워크 위치에서 GPS 표시 Android
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;
public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;
if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
String provider = lm.getBestProvider(criteria, true);
Location net_loc = null, gps_loc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(provider);
if (network_enabled)
net_loc = lm.getLastKnownLocation(provider);
// if there are both values use GPS
if (gps_loc != null && net_loc != null) {
locationResult.gotLocation(gps_loc);
return;
}
if (gps_loc != null) {
locationResult.gotLocation(gps_loc);
return;
}
if (net_loc != null) {
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}
public static abstract class LocationResult {
public abstract void gotLocation(Location location);
}
도움이 될 경우 위치를 얻으려는 전체 코드를 표시하기 위해 원래의 게시물을 편집했습니다. 내가 게시 한 코드가 내가 생각했던 것을하지 않는 것이 두려웠습니다. 나는 타이머에 대해서도 궁금해했다. 나는 그것이 ICS에서 잘 작동하는 것으로 나타 났지만, 그것이 Jelly Bean 장치에 설치되었을 때, 앱이 강제로 닫히고있다. – Jasonwilliams10