Reputation: 39
I am starting a thread from an android service that constantly uploads a file using a tcp connection to a server at constant intervals. The thread has a while(true) loop that performs this. The operations get performed for sometime but then it stops. I tried using the adb logcat to debug and saw certain messages I printed inside the thread for sometime only. After that they also stopped. Further when I stopped the service I got a NullPointerException since in the onDestroy() method of the service I had used the thread object.
Does the thread get killed and not run as long as the service runs? Do I need to do this while(true) operation in onStart() of service class itself and not spawn a new thread?
Any solution provided will be of great help.
Code is as follows (partly based on this answer by kuester2000):
public void run()
{
Log.d("TAG","Starting Thread");
String data = "";
updateServer();
while(flag)
{
String path = extStorageDirectory + "/" + appFolder + "/" + "SAT_pingLog_" + Long.toString(System.currentTimeMillis()) + ".txt";
createFile(path);
int count = 0;
while(flag && count < 32768)
{
data = "";
String result = Long.toString(getLatency(url));
data = Long.toString(System.currentTimeMillis()) + " " + gps[0] + " " + gps[1] + " " + strength + " " + result;
Log.d("DEBUGGING",data);
writeToLog(path,data);
try
{
Thread.sleep(sleepTime);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
}
updateServer();
}
}public long getLatency(String Url)
{
long startTime = 0, endTime = 0, latency = 0;
try
{
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 10000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpHead httphead = new HttpHead(Url);
//HttpParams httpParameters = new BasicHttpParams();
//HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
//HttpConnectionParams.setSoTimeout(httpParameters, 5000);
//HttpClient httpClient = new DefaultHttpClient(httpParameters);
//request.setURI(new URI("http://www.google.com"));
System.out.println("Executing request " + httphead.getURI());
Log.d("DEBUGGING","STARTING EXECUTE");
startTime = System.currentTimeMillis();
HttpResponse response = httpclient.execute(httphead);
endTime = System.currentTimeMillis();
Log.d("DEBUGGING","ENDING EXECUTE");
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK)
latency = endTime - startTime;
else
latency = 0;
}
catch(Exception e)
{
Log.d("DEBUGGING","EXCEPTION CAUGHT");
e.printStackTrace();
}
Log.d("DEBUGGING","LATENCY:"+Long.toString(latency));
return latency;
}
}
Upvotes: 2
Views: 515
Reputation: 1006744
Does the thread get killed and not run as long as the service runs?
Android components are oblivious to threads they did not create. So while IntentService
will deal with the thread it created for the use of onHandleIntent()
, a regular Service
will pay no attention to any threads you fork yourself.
Once a service is destroyed, any threads it leaks will continue to run, until such time as Android terminates the process.
If your threads are stopping unexpectedly, that has nothing to do with Service
-- again, Service
knows nothing of threads that you fork yourself. Make sure that you are not silently eating exceptions by having empty catch
blocks, as my guess is that something is raising an exception, causing you to exit your loop, but that you aren't logging it for some reason.
Upvotes: 3