1

我想调用一个活动来获取当前的 gsp 位置并返回经度和纬度,但没有任何反应,所以我尝试了编程书中的以下简单代码:

{private static final String TAG = GetGps.class.getSimpleName();

private LocationManager manager;
private LocationListener listener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps);
    // LocationManager-Instanz ermitteln
    manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    // Liste mit Namen aller Provider erfragen
    List<String> providers = manager.getAllProviders();
    // Infos zu Location Providern ausgeben
    for (String name : providers) {
        LocationProvider lp = manager.getProvider(name);
        Log.d(TAG,
                lp.getName() + " --- isProviderEnabled(): "
                        + manager.isProviderEnabled(name));
        Log.d(TAG, "requiresCell(): " + lp.requiresCell());
        Log.d(TAG, "requiresNetwork(): " + lp.requiresNetwork());
        Log.d(TAG, "requiresSatellite(): " + lp.requiresSatellite());
    }
    // Provider mit grober Auflösung
    // und niedrigen Energieverbrauch
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    // Namen ausgeben
    String name = manager.getBestProvider(criteria, true);
    Log.d(TAG, name);
    // LocationListener-Objekt erzeugen
    listener = new LocationListener() {
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            Log.d(TAG, "onStatusChanged()");
        }

        public void onProviderEnabled(String provider) {
            Log.d(TAG, "onProviderEnabled()");
        }

        public void onProviderDisabled(String provider) {
            Log.d(TAG, "onProviderDisabled()");
        }

        public void onLocationChanged(Location location) {
            Log.d(TAG, "onLocationChanged()");
            if (location != null) {
                String s = "Breite: " + location.getLatitude()
                        + "\nLänge: " + location.getLongitude();
                Toast.makeText(GetGps.this,
                        s,
                        Toast.LENGTH_LONG).show();
            }
        }
    };
}

@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG, "onStart()");
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0,
            listener);
}

@Override
protected void onPause() {
    super.onPause();
    Log.d(TAG, "onPause()");
    manager.removeUpdates(listener);
}}

正如我所说,没有任何反应,所以这里是目录:

12-05 15:59:54.490: D/GetGps(6833): network --- isProviderEnabled(): true
12-05 15:59:54.490: D/GetGps(6833): requiresCell(): true
12-05 15:59:54.490: D/GetGps(6833): requiresNetwork(): true
12-05 15:59:54.490: D/GetGps(6833): requiresSatellite(): false
12-05 15:59:54.500: D/GetGps(6833): passive --- isProviderEnabled(): true
12-05 15:59:54.500: D/GetGps(6833): requiresCell(): false
12-05 15:59:54.500: D/GetGps(6833): requiresNetwork(): false
12-05 15:59:54.500: D/GetGps(6833): requiresSatellite(): false
12-05 15:59:54.500: D/GetGps(6833): gps --- isProviderEnabled(): true
12-05 15:59:54.500: D/GetGps(6833): requiresCell(): false
12-05 15:59:54.500: D/GetGps(6833): requiresNetwork(): true
12-05 15:59:54.500: D/GetGps(6833): requiresSatellite(): true
12-05 15:59:54.500: D/GetGps(6833): network
12-05 15:59:54.500: D/GetGps(6833): onStart()
12-05 15:59:54.530: D/OpenGLRenderer(6833): Flushing caches (mode 0)

有人可以帮我吗?

4

0 回答 0