を使用する簡単なアプリケーションがありますLocationClient
。Wi-Fi が有効になるまで位置情報が更新されます。
ケース:
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"/>