0

このコードを使用して場所を取得します。

final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        final Criteria locationCritera = new Criteria();
        locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
        locationCritera.setAltitudeRequired(false);
        locationCritera.setBearingRequired(false);
        locationCritera.setCostAllowed(true);
        locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

        final String providerName = locationManager.getBestProvider(locationCritera, true);

        if (providerName != null && locationManager.isProviderEnabled(providerName)) {
            locationManager.requestLocationUpdates(providerName, 20000, 100, this);
        } else {
            Toast.makeText(this, "GPS turned off!", Toast.LENGTH_LONG).show();
            final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }

ACCESS_FINE_LOCATION と ACCESS_COARSE_LOCATION の両方のアクセス許可がマニフェストで定義されています。

ネットワークと GPS の両方がオンになっていますが、onProviderEnabled のみがパラメーター「ネットワーク」で呼び出されます (1 回)。しかし、同じコードが 4.0 エミュレーターと Onda 4.1 Tab で正常に動作します。なんで?

4

1 に答える 1

1

GPSの例があります。多分あなたは参照としてそれを使用することができます: 例はここにあります:

于 2013-04-19T01:03:47.080 に答える