无法获得GPS位置

时间:2014-09-03 04:47:10

标签: android android-location

我想要用户位置。网络和GPS位置。我正在获得网络位置,但GPS位置始终为空。这是我为此写的代码。

public class GetLocation extends Service implements LocationListener {

    LocationManager locationManager;
    Location networkLocation, gpsLocation;
    Intent intentBroadcastLocationUpdate;
    boolean gpslocationOn, networklocationOn;
    Handler handler = new Handler();

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            broadcastLocation();

            handler.postDelayed(this, 3000);
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        intentBroadcastLocationUpdate = new Intent("UPDATE_LOCATION");

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1000 * 60 * 1, this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 1000 * 60 * 1, this);

        networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        networklocationOn = true;
        gpslocationOn = true;

        broadcastLocation();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handler.postDelayed(runnable, 3000);
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
        Toast.makeText(getApplicationContext(),
            location.getProvider() + " Location Changed:" + location.getLatitude() + " , "
                + location.getLongitude(), Toast.LENGTH_LONG).show();
    };

    @Override
    public void onProviderDisabled(String arg0) {
        Toast.makeText(getApplicationContext(), arg0 + " Disabled", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        if (provider.equals("network")) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 1000 * 60 * 1, this);
        }
        if (provider.equals("gps")) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1000 * 60 * 1, this);
        }
        Toast.makeText(getApplicationContext(), provider + " Enabled", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        locationManager.removeUpdates(this);
        handler.removeCallbacks(runnable);    
    }

    private void broadcastLocation() {
        if (gpsLocation == null && networkLocation == null) {
            Toast.makeText(getApplicationContext(),
                    "on create Couldn't get user location", Toast.LENGTH_LONG).show();
            intentBroadcastLocationUpdate.putExtra("loc", "Could not retrive location");
            sendBroadcast(intentBroadcastLocationUpdate);
        } else if (gpsLocation != null) {
            intentBroadcastLocationUpdate.putExtra("loc", "GPS:" + gpsLocation.getLatitude() + ","
                    + gpsLocation.getLongitude());
            sendBroadcast(intentBroadcastLocationUpdate);
        } else if (networkLocation != null) {
            intentBroadcastLocationUpdate.putExtra("loc", "Net:" + networkLocation.getLatitude() + ","
                    + networkLocation.getLongitude());
            sendBroadcast(intentBroadcastLocationUpdate);
        } else if (gpsLocation != null && networkLocation != null) {
            intentBroadcastLocationUpdate.putExtra(
                    "loc",
                    "Net:" + networkLocation.getLatitude() + ","
                            + networkLocation.getLongitude() + " AND GPS:"+ gpsLocation.getLatitude() + ","
                            + gpsLocation.getLongitude());
            sendBroadcast(intentBroadcastLocationUpdate);
        }
    }

}

有谁能告诉我我在做什么?为什么我无法获得GPS定位?获取GPS位置的最佳做法是什么?

2 个答案:

答案 0 :(得分:0)

在清单中添加这些权限

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

参考this tutorial

答案 1 :(得分:0)

public void getMyLocation() {
LocationManager locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);

    Location l = null;
    for (int i = 0; i < providers.size(); i++) {
        l = locationManager.getLastKnownLocation(providers.get(i));
        if (l != null)
            break;
    }
    if (l != null) {
        latitude = l.getLatitude();
        longitude = l.getLongitude();
    }
}