我希望在一段时间间隔内获取用户的位置。我尝试融合位置。但是在手机进入睡眠模式后它会返回相同的位置。
public class FusedActivity extends Activity implements OnClickListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
private static final long FIVE_MINUTES = 300000;
private LocationClient locationclient;
private LocationRequest locationrequest;
private Intent mIntentService;
private PendingIntent mPendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
itemsFindViewById();
mIntentService = new Intent(this, LocationService.class);
mPendingIntent = PendingIntent.getService(this, 1, mIntentService, 0);
int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resp == ConnectionResult.SUCCESS) {
locationclient = new LocationClient(this, this, this);
locationclient.connect();
log.i(TAG, "ConnectionResult.SUCCESS");
} else {
Toast.makeText(this, "Google Play Service Error " + resp,
Toast.LENGTH_LONG).show();
}
}
private void startLocationUpdates() {
if (locationclient != null) {
locationrequest = LocationRequest.create();
locationrequest.setInterval(FIVE_MINUTES);
locationrequest.setFastestInterval(FIVE_MINUTES);
locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationclient.requestLocationUpdates(locationrequest,
mPendingIntent);
startStopBtn.setText("Stop");
log.i(TAG, " register location updates");
} else
log.i(TAG, "location client null");
}
private void stopLocationUpdates() {
if (locationclient != null) {
locationclient.removeLocationUpdates(mPendingIntent);
log.i(TAG, " Unregister location updates");
startStopBtn.setText("Start");
}
}
//意向服务
意图服务类......
public class LocationService extends IntentService {
private static final String TAG = "LocationService";
Logger log;
public LocationService() {
super("Fused Location");
log = Logger.getInstance(this);
}
@Override
protected void onHandleIntent(Intent intent) {
Location location = intent
.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);
if (location != null) {
Toast.makeText(this, "Received Location Update", Toast.LENGTH_SHORT)
.show();
getBatteryLevel();
StringBuilder sb = new StringBuilder();
StringBuilderPrinter sbp = new StringBuilderPrinter(sb);
location.dump(sbp, "");
log.i(TAG, sb.toString());
}
}
private void getBatteryLevel() {
float lev;
Intent batteryIntent = registerReceiver(null, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
if (level == -1 || scale == -1) {
lev = 50.0f;
} else {
lev = ((float) level / (float) scale) * 100.0f;
}
log.i(TAG, "Battery Level" + lev);
}
}
日志
这是日志...
I 05-12-13 19:40:37.566+0530 FusedActivityFusedActivity Created
I 05-12-13 19:40:37.582+0530 FusedActivityConnectionResult.SUCCESS
I 05-12-13 19:40:37.684+0530 FusedActivity onConnected
I 05-12-13 19:55:08.582+0530 FusedActivity register location updates
I 05-12-13 19:55:08.615+0530 LocationServiceBattery Level82.0
I 05-12-13 19:55:08.620+0530 LocationServicemProvider=fused mTime=1386253508567
mLatitude=12.9785261 mLongitude=80.2296999
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=655.0
mExtras=null
I 05-12-13 20:00:09.433+0530 LocationServiceBattery Level82.0
I 05-12-13 20:00:09.436+0530 LocationServicemProvider=fused mTime=1386253809354
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:05:10.473+0530 LocationServiceBattery Level81.0
I 05-12-13 20:05:10.481+0530 LocationServicemProvider=fused mTime=1386254110368
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:10:12.477+0530 LocationServiceBattery Level81.0
I 05-12-13 20:10:12.479+0530 LocationServicemProvider=fused mTime=1386254412369
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:15:14.482+0530 LocationServiceBattery Level81.0
I 05-12-13 20:15:14.487+0530 LocationServicemProvider=fused mTime=1386254714360
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:20:18.442+0530 LocationServiceBattery Level80.0
I 05-12-13 20:20:18.444+0530 LocationServicemProvider=fused mTime=1386255018354
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:25:18.583+0530 LocationServiceBattery Level80.0
I 05-12-13 20:25:18.590+0530 LocationServicemProvider=fused mTime=1386255318418
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:30:19.439+0530 LocationServiceBattery Level80.0
I 05-12-13 20:30:19.441+0530 LocationServicemProvider=fused mTime=1386255619342
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:35:21.478+0530 LocationServiceBattery Level80.0
I 05-12-13 20:35:21.482+0530 LocationServicemProvider=fused mTime=1386255921356
mLatitude=12.9811803 mLongitude=80.2320268
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=964.0
mExtras=null
I 05-12-13 20:40:26.732+0530 LocationServiceBattery Level79.0
I 05-12-13 20:40:26.742+0530 LocationServicemProvider=fused mTime=1386256226616
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:45:29.515+0530 LocationServiceBattery Level79.0
I 05-12-13 20:45:29.516+0530 LocationServicemProvider=fused mTime=1386256529414
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:50:32.442+0530 LocationServiceBattery Level79.0
I 05-12-13 20:50:32.445+0530 LocationServicemProvider=fused mTime=1386256832334
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 20:55:35.539+0530 LocationServiceBattery Level79.0
I 05-12-13 20:55:35.541+0530 LocationServicemProvider=fused mTime=1386257135439
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 21:00:38.489+0530 LocationServiceBattery Level78.0
I 05-12-13 21:00:38.491+0530 LocationServicemProvider=fused mTime=1386257438383
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 21:05:40.601+0530 LocationServiceBattery Level78.0
I 05-12-13 21:05:40.603+0530 LocationServicemProvider=fused mTime=1386257740373
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 21:10:42.440+0530 LocationServiceBattery Level78.0
I 05-12-13 21:10:42.442+0530 LocationServicemProvider=fused mTime=1386258042334
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 21:15:46.011+0530 LocationServiceBattery Level78.0
I 05-12-13 21:15:46.013+0530 LocationServicemProvider=fused mTime=1386258345906
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 21:20:49.474+0530 LocationServiceBattery Level78.0
I 05-12-13 21:20:49.475+0530 LocationServicemProvider=fused mTime=1386258649342
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 21:25:53.515+0530 LocationServiceBattery Level78.0
I 05-12-13 21:25:53.520+0530 LocationServicemProvider=fused mTime=1386258953407
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 21:30:54.393+0530 LocationServiceBattery Level77.0
I 05-12-13 21:30:54.395+0530 LocationServicemProvider=fused mTime=1386259254329
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null
I 05-12-13 21:35:55.496+0530 LocationServiceBattery Level77.0
I 05-12-13 21:35:55.498+0530 LocationServicemProvider=fused mTime=1386259555342
mLatitude=12.9806657 mLongitude=80.2298551
mHasAltitude=false mAltitude=0.0
mHasSpeed=false mSpeed=0.0
mHasBearing=false mBearing=0.0
mHasAccuracy=true mAccuracy=1035.0
mExtras=null