问题描述
|
在此应用程序中,我正在运行一个远程服务,在该服务中,我正在启动一个单独的线程来运行TCP连接...在应用程序退出时,我必须关闭输入流,为此我要给它提供线程中断并更改布尔值(stopbroadcastRequested) ..当在线程块中满足该条件时,我正在关闭inputstream,但实际上并没有关闭...如果我在正常线程执行过程中手动在线程内提供instream.close,则输入流将按预期关闭。但是在if(stopbroadcastRequested){代码块}中,它不起作用..有人可以告诉我我犯了什么错误吗?
public class broadcastService extends Service {
class Task implements Runnable{
OutputStream outStream = null;
InputStream inStream = null;
@Override
public void run() {
while(!stopbroadcastRequested){
Log.i(TAG,\"Thread Task started\");
try {
isSocketopen = broadCastComm.isAliveOrOpenSocket(\"192.168.43.2\",6000,17,0);
if(isSocketopen){
Log.d(\"SERVICE CLASS\",\"STARTED THREAD - Writing in output stream\");
notificationMngr.cancelAll();
isShowingNotification = false;
outStream = broadCastComm.getCurrentOutputStream();
outStream.write(messagetoBeSent);
if(Integer.valueOf(messagetoBeSent[2]) != (byte)0xA0){
Log.e(\"REVERTING\",\"REVERTING\");
messagetoBeSent = mFormatter.formbroadCastMessage(\"GET_PERIPH_DATA\");
}
Log.d(\"OUTPUT STREAM\",\"Message sent ->\" + ByteArrayToString(messagetoBeSent));
}else{
connectivityStatusHandler.sendEmptyMessage(0);
}
Thread.sleep(3000L);
if(isSocketopen){
}
} catch (Throwable t) {
Log.e(TAG,\"Failed to retrieve data in thread\",t);
}
Log.d(\"SERVICE CLASS\",\"End of THREAD\");
}
if(stopbroadcastRequested){
Log.e(\"SERVICE\",\"STOPPED THREAD\");
try {
Log.e(\"*****THREAD\",\"CLOSED INPUT STARTED\");
if(inStream != null)
inStream.close();
Log.e(\"*****THREAD\",\"CLOSED INPUT CLOSED\");
outStream.flush();
outStream.close();
Log.e(\"*****THREAD\",\"CLOSED OUTPUT\");
} catch (Exception e) {
Log.e(\"THREAD\",\"Failed TO CLOSE STREAMS\");
}
}
}
public synchronized void stopThread(){
stopbroadcastRequested = true;
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG,\"Service destroying\");
stopbroadcastRequested = true;
serviceThread.interrupt();
cleanNotifications();
broadCastComm.clearConnections();
//dbHandler.removeCallbacks(dbupdater);
try {
dbHelper.cleanup();
} catch (Exception e) {
Log.e(\"SERVICE\",\"Failed to clear DB connections\");
}
}
}
------LOG
05-30 19:28:03.878: ERROR/broadcastService(20288): Failed to retrieve data in thread
05-30 19:28:03.878: ERROR/broadcastService(20288): java.lang.InterruptedException
05-30 19:28:03.878: ERROR/broadcastService(20288): at java.lang.VMThread.sleep(Native Method)
05-30 19:28:03.878: ERROR/broadcastService(20288): at java.lang.Thread.sleep(Thread.java:1213)
05-30 19:28:03.878: ERROR/broadcastService(20288): at java.lang.Thread.sleep(Thread.java:1195)
05-30 19:28:03.878: ERROR/broadcastService(20288): at com.RBEI.TTApp.broadcastService$Task.run(broadcastService.java:126)
05-30 19:28:03.878: ERROR/broadcastService(20288): at java.lang.Thread.run(Thread.java:1019)
05-30 19:28:03.878: DEBUG/SERVICE CLASS(20288): End of THREAD
05-30 19:28:03.878: ERROR/SERVICE(20288): STOPPED THREAD
05-30 19:28:03.878: ERROR/*****THREAD(20288): CLOSED INPUT STARTED
05-30 19:28:03.882: ERROR/*****THREAD(20288): CLOSED INPUT CLOSED
05-30 19:28:03.882: ERROR/*****THREAD(20288): CLOSED OUTPUT
解决方法
您只能使用一次close()。只需添加一张支票
if (inStream != null { inStream.close(); )
每次。
, 您在第一个行收到异常(InterruptedException)
Thread.sleep(3000L);
在您甚至为inStream变量分配值之前,都会发生这种情况,
所以
inStream = broadCastComm.getCurrentInputStream();
永远不会执行。您无法关闭null的Socket。
, 抱歉,问题是我没有正确地解除服务的绑定,这导致了所有这些问题...没有在Tabactivity中使用正确的上下文来解除绑定..不得不使用getApplicationContext()代替... :)