3

Android 4.2 OS電話でアプリを実行すると、getLAstKnownLocationから返される値を取得できないようです(常にnull)。奇妙なことに、Mapviewは正常に機能し、現在の場所が表示されます。

この問題を解決する方法はありますか?

これが私のコードです

onCreateで:

mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = mlocManager.getBestProvider(criteria, false);
mlocManager.requestLocationUpdates(provider, 0, 0, geoHandler);

次にリスナー:

public class GeoUpdateHandler implements LocationListener {
    public void onLocationChanged(Location location) {
        // called when the listener is notified with a location update from the GPS
        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);
        new GeoPoint(lat, lng);

        Log.d(this.getClass().getSimpleName(), "onLocationChanged " + lat);
        //          mlocManager.removeUpdates(this);
    }

    public void onProviderDisabled(String provider) {
        // called when the GPS provider is turned off (user turning off the GPS on the phone)
        Log.d(TAG, "DISABLED");
    }

    public void onProviderEnabled(String provider) {
        // called when the GPS provider is turned on (user turning on the GPS on the phone)
        Log.d(TAG, "ENABLED");
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // called when the status of the GPS provider changes
        Log.d(TAG, "CHANGED");
    }
}
4

1 に答える 1

0

getLastKnownLocationは、プロバイダーが無効になっている場合にnullを返します。または、LastKnownLocationがない場合。

オンライン:provider = mlocManager.getBestProvider(criteria, false);

プロバイダーは、無効になっているプロバイダーを含むBestProviderを要求しています。ステートメントの最後でtrueを使用してみて、それが機能するかどうかを確認してください。

http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)

于 2012-12-16T02:08:12.300 に答える