I wrote a location listener class and it registers updates for 30000 miliseconds and 100 meters. It collects data to sharedPreferences
for 10 minutes then send whole data and clears the Shared key.
However they said that application does not send data after 2,3 hours? The application works in the background and is not a service. Here is the code:
MainActivity.class
LocManager locMan= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locMan = new LocManager(MainActivity.this);
}
LocationManager.java
public class LocManager implements LocationListener {
Context cnx;
String glbData = "";
LocationManager manager = null;
public boolean sendOnly1Record = false;
public int saveUserApplicationStatusCalled = 0;
Date firstDate = null;
Date currDate = null;
String USR_ID = "", USR_PASSWORD = "", USR_DOMAIN = "";
private boolean isUploadingNow = false;
public LocManager(Context cnx) {
this.cnx = cnx;
manager = (LocationManager) this.cnx
.getSystemService(Context.LOCATION_SERVICE);
SharedPreferences sh = cnx.getSharedPreferences(Constants.SH_GLB_DATA,
Context.MODE_PRIVATE);
glbData = sh.getString(Constants.SH_GLB_KEY, "");
USR_ID = sh.getString(Constants.SH_USR_ID_KEY, "nil");
USR_PASSWORD = sh.getString(Constants.SH_USR_PASS_KEY, "nil");
USR_DOMAIN = sh.getString(Constants.SH_USR_DOMAIN_KEY, "nil");
firstDate = new Date();
}
public void startUpdate() {
Criteria myCriteria = new Criteria();
myCriteria.setAccuracy(Criteria.ACCURACY_FINE);
myCriteria.setPowerRequirement(Criteria.POWER_LOW);
String myProvider = manager.getBestProvider(myCriteria, true);
long minTimeMillis = 30000;
manager.requestLocationUpdates(myProvider, minTimeMillis, 100, this);
}
@Override
public void onLocationChanged(Location location) {
if (sendOnly1Record) {
sendOnly1Record = false;
funcSendOnly1Record(location);
} else if (!isUploadingNow) {
String str_date = getCurrentStrDate();
String str_battery_level = "" + getBatteryLevel();
String str_lat = "" + location.getLatitude();
String str_lon = "" + location.getLongitude();
long interval = getDateDiffsinMinutes();
if (interval > 10) {
isUploadingNow = true;
postData();
firstDate = new Date();
} else {
StringBuffer buf = new StringBuffer();
buf.append(str_date);
buf.append("|");
buf.append(str_lat);
buf.append("|");
buf.append(str_lon);
buf.append("|");
buf.append(str_battery_level);
buf.append("|");
buf.append("" + saveUserApplicationStatusCalled);
buf.append("|");
glbData = glbData + buf.toString();
SharedPreferences sh = cnx.getSharedPreferences(
Constants.SH_GLB_DATA, Context.MODE_PRIVATE);
SharedPreferences.Editor ed = sh.edit();
ed.putString(Constants.SH_GLB_KEY, glbData);
ed.commit();
if (saveUserApplicationStatusCalled == 0) {
saveUserApplicationStatusCalled = 1;
}
}
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
private void postData() {
//Posts the collected data
}
};
public void funcSendOnly1Record(Location location) {
String str_date = getCurrentStrDate();
String str_battery_level = "" + getBatteryLevel();
String str_lat = "" + location.getLatitude();
String str_lon = "" + location.getLongitude();
StringBuffer buf = new StringBuffer();
buf.append(str_date);
buf.append("|");
buf.append(str_lat);
buf.append("|");
buf.append(str_lon);
buf.append("|");
buf.append(str_battery_level);
buf.append("|");
buf.append("" + saveUserApplicationStatusCalled);
buf.append("|");
//post one data
}
private int getBatteryLevel() {
Intent batteryIntent = cnx.registerReceiver(null, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
// Error checking that probably isn't needed but I added just in case.
if (level == -1 || scale == -1) {
return 50;
}
float x = ((float) level / (float) scale) * 100.0f;
return (int) x;
}
private long getDateDiffsinMinutes() {
currDate = new Date();
long result = ((currDate.getTime() / 60000) - (firstDate.getTime() / 60000));
return (int) result;
}
private String getCurrentStrDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String str_date = dateFormat.format(date);
return str_date;
}
}
Your background process is being terminated to free up memory for other applications. This is perfectly normal and expected behavior.