没有Wifi定位不起作用

时间:2013-09-10 13:00:40

标签: java android geolocation location google-maps-android-api-2

我有一个使用LocationClient的简单应用程序。位置已更新,直到启用wifi。

例:

1. 3G on, wifi on (connected or not), GPS on- work
2. 3G on, wifi on (connected or not), GPS off- work
3. 3G off, wifi on (connected or not), GPS off- work
4. 3G on/off, wifi off, GPS on/off- doesn't work 

代码:

public class PlayPositionProvider implements PositionProvider, FastestUpdate, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

    private static final String LOG_TAG = "PLAY_POSITION_PROVIDER";
    private long period, fastest;
    private LocationClient client;
    private final Context context;
    private final PositionListener positionListener;
    private LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {

            Log.v(LOG_TAG, "LOCATION UPDATE");

            positionListener.onPositionUpdate(location);
        }
    };

    public PlayPositionProvider(Context context, long period, long fastest, PositionListener positionListener) {
        this.context = context;
        this.positionListener = positionListener;
        this.period = period;
        this.fastest = fastest;
    }

    @Override
    public void setUpdateInterval(long interval) {

        request.setInterval(interval);
        this.period = interval;
    }

    @Override
    public void setFastestInterval(long fastestInterval) {

        request.setFastestInterval(fastestInterval);
        this.fastest = fastestInterval;
    }

    private final LocationRequest request = LocationRequest.create();

    @Override
    public void onConnected(Bundle bundle) {

        Log.v(LOG_TAG, "onConnected()");

        request.setInterval(period);
        request.setFastestInterval(fastest);

        request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        client.requestLocationUpdates(request, listener);
    }

    @Override
    public void onDisconnected() {
        Log.v(LOG_TAG, "onDisconnected()");
        client.removeLocationUpdates(listener);
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.v(LOG_TAG, "NOT IMPLEMENTED onConnectionFailed()");

    }

    @Override
    public void startUpdates() {

        client = new LocationClient(context, this, this);
        client.connect();
    }

    @Override
    public void stopUpdates() {

        client.disconnect();

    }
}

AndroidManifest.xml我有以下许可:

<uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17"/>

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.BATTERY_STATS"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="org.[PACKAGE_NAME].permission.C2D_MESSAGE"
            android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

<!--Only in debug purpose-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_LOGS"/>

<permission android:name="org.[PACKAGE_NAME].permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="org.[PACKAGE_NAME].permission.C2D_MESSAGE"/>

1 个答案:

答案 0 :(得分:0)

两件事:

  1. 如果GooglePlayServices可用,我们根本不知道你是否在检查..
  2. 使用requestLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);表示您对GPS数据感兴趣..我不确定但这可能是问题