I am developing an app that gets current latlongs of a device and stores them in a file in a new line. I ran my app on kit kat version 4.4.4 and on marshmallow version 6.0.1. I took both device in my car for testing and took a long ride around the city. I noticed a weird output, the file generated from kit kat had 576 lines means that 576 pair of latlongs were recorded. But in the file generated on marshmallow device had only 250 lines of latlongs. As long as I know, according to the definition of Doze mode introduced in marshmallow, the phone only enters doze mode when the device is stationary. But in my case the device was not stationary because it was in a moving car. I am confused. Can anyone help.
Below is my Background Service Class without file saving code.
public class GPSTrackerService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
mSettingsClient = LocationServices.getSettingsClient(getApplicationContext());
createLocationCallback();
createLocationRequest();
buildLocationSettingsRequest();
new CountDownTimer(9900000,3000){
@Override
public void onTick(long l) {
Log.d("BOSS_DK","On Tick");
}
@Override
public void onFinish() {
}
}.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@TargetApi(Build.VERSION_CODES.M)
public void test(){
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
startActivity(intent);
}
private LocationRequest mLocationRequest;
private LocationSettingsRequest mLocationSettingsRequest;
private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;
private SettingsClient mSettingsClient;
// Start Fused Location services
protected void createLocationRequest() {
// create the locatioon request and set parameters
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(2000); // will return location after every 5 seconds
mLocationRequest.setFastestInterval(1000); // fastest rate app can handle updates
//mLocationRequest.setSmallestDisplacement(5);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void buildLocationSettingsRequest() {
// get current locations settings of user's device
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
startLocationUpdates();
}
private void startLocationUpdates() {
// if settings are satisfied initialize location requests
mSettingsClient.checkLocationSettings(mLocationSettingsRequest).addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
//locUpdates = true;
// All location settings are satisfied.
//noinspection MissingPermission - this comment needs to stay here to stop inspection on next line
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
})
// if settings need to be changed prompt user
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
}
// stop location updates
private void stopLocationUpdates() {
//locUpdates = false;
mFusedLocationClient.removeLocationUpdates(mLocationCallback).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
}
});
}
private void createLocationCallback() {
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
List<Location> locations = locationResult.getLocations();
/*if(locationResult.getLastLocation().hasAccuracy()) {
setLatLong(locationResult.getLastLocation());
}*/
for(Location location : locations){
setLatLong(location);
}
}
};
}
// Set new latitude and longitude based on location results
public void setLatLong(Location location) {
Log.d("BOSS_DK",String.valueOf(location.getLatitude())+","+String.valueOf(location.getLongitude()));
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("BOSS_DK","service destroyed");
}
}