如果网络不可用,我的 android 应用程序会在 5 到 10 分钟内在后台被杀死。
App在后台不断调用一些rest服务。
我的 logcat 没有任何异常。
但是当我回到前台时,我的应用程序从头开始,因为它被操作系统杀死了。
请建议我可能是什么原因以及我应该怎么做才能防止它。
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();
}
}
这是即使网络不可用也与服务器交互的方法
下面是线程调用休息服务的代码。
如何对服务做同样的事情:
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();
}
}
谢谢