My android app got killed in background within 5 to 10 minutes if network is not available.
App constantly calls some rest services in background.
I am not getting any exception in my logcat.
But when I comes back in foreground my app starts from beginning because its killed by OS.
Please suggest me what might be the cause and What should I do to prevent it.
public void sendHttpPost() throws ClientProtocolException, IOException{
HttpPost httpPostRequest = new HttpPost(url + buildParams());
// add headers
HttpParams httpParams = new BasicHttpParams();
Iterator it = headers.entrySet().iterator();
Iterator itP = params.entrySet().iterator();
while (it.hasNext()) {
Entry header = (Entry) it.next();
httpPostRequest.addHeader((String)header.getKey(), (String)header.getValue());
}
while(itP.hasNext())
{
Entry header = (Entry) itP.next();
StringEntity se = new StringEntity((String) header.getValue());
httpPostRequest.setEntity(se);
}
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
HttpConnectionParams.setSoTimeout(httpParams, 30000);
httpPostRequest.setParams(httpParams);
HttpClient client = new DefaultHttpClient();
HttpResponse resp;
resp = client.execute(httpPostRequest);
this.respCode = resp.getStatusLine().getStatusCode();
// Log.i(TAG, "response code: " + getResponseCode());
this.responsePhrase = resp.getStatusLine().getReasonPhrase();
// Log.i(TAG, "error msg: " + getErrorMsg());
HttpEntity entity = resp.getEntity();
if (entity != null){
InputStream is = entity.getContent();
//Header contentEncoding = resp.getFirstHeader("Content-encoding");
//Log.i(TAG, "endoding" + contentEncoding.getValue());
response = convertStreamToString(is);
//response = response.substring(1,response.length()-1);
//response = "{" + response + "}";
// Log.i(TAG, "response: " + response);
screen.update("Login", response, false);
is.close();
}
}
This is the method which interact with server even if network is not available
and below is the code where a thread calls rest services.
How to do same with services:
public void start()
{
try{
if(_exit)
return;
//_lastExecute = System.currentTimeMillis();
_try++;
// Log.i("-----HK Requests--------- \n\r", _url);
final String response = HttpConnector.callWebService(_url, _data, _listener, _token);
CallBackThread ct = new CallBackThread(_action, response, autocallreq);
ct.start();
// while(ct.isAlive())
// {
// Thread.sleep(500);
// }
}
catch(Exception e)
{
System.out.print("error="+e.getMessage());
Helper.LogException(this, e);
//CallBackThread ct = new CallBackThread(_action, "", autocallreq);
//ct.start();
}
}
Thanks
that’s what suppose to happen when your app don’t have any forground
Activityor forgroundServiceinstances.that’s also depends on how much memory you app consuming in background – the OS will put your app in high priority to be “killed” first if consumes a lot of memory.
this Google IO video explains more about when your app could be killed in such situation – http://www.youtube.com/watch?v=gbQb1PVjfqM&list=PL4C6BCDE45E05F49E&index=10&feature=plpp_video
that’s two or three minutes discussing about this issue in the middle of the video
if you are not doing the calls to the REST methods from android
Serviceclass – you should do it, because as I said – the OS will kill your process very fast if no Activity is forground or no forground Service is running,I must say that running background service that doing for long period of time network operations in background – is a bad idea anyway: it will consume a lot of battery, and also 3G bandwidth. users will delete your app very fast after they will realize it draining thier battery. so you should be carful from doing too much in background