如果網絡不可用,我的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對象中運行代碼? –