如果网络不可用,我的android应用在5到10分钟内就会在后台死亡。如果网络不可用,Android应用在后台死亡
应用程序不断地在后台调用一些休息服务。
我没有得到任何异常在我的logcat。
但是,当我回到前台时,我的应用程序从开始就开始,因为它被OS所杀。
请告诉我可能是什么原因以及我该怎么做才能阻止它。
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();
}
}
这是与服务器即使网络不可用
以下是其中一个线程调用REST服务代码交互的方法。
如何做相同的服务:
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();
}
}
感谢
你是否尝试通过网络发送的东西,如果它可用?然后检查你的应用程序是否尝试发送,即使网络不可用... – Ekonion
请分享一些代码,以便我们可以理解你的问题究竟是什么 – Alpay
你是什么意思的背景?你是否在Service或Thread对象中运行代码? –