我的应用程序中有一个服务(由一个线程创建),在我内部创建另一个线程,这是一个TCP服务器。 启动线程后短时间(并完全正常工作,接收和处理收到的软件包)服务被“超时执行服务”和我服务中的ANR kill 。
这是我的服务代码和正在完成所有工作的线程:
public class Android_Wifi_Service extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
handleStart();
return START_STICKY;
}
private void handleStart() {
Server server=new Server(this);
new Thread(server).run();
}
@Override
public void onDestroy(){
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是我的主题:
public class Server implements Runnable {
private boolean end=false;
private Context context;
public Server(Context _context){
this.context=_context;
}
@Override
public void run() {
try {
ServerSocket ss= new ServerSocket(5552);
while(!end){
//wait for connection
System.out.println("Server Waiting connection");
Socket s=ss.accept();
SenderMessage msg=null;
// connected
InputStream is = s.getInputStream();
ObjectInputStream obj=new ObjectInputStream(is);
try {
msg= ( SenderMessage ) obj.readObject();
} catch (Exception e) {
Log.e("Message Receive Error", e.toString());
}
obj.close();
is.close();
s.close();
if(msg!=null){
// do somethings
}
}
} catch (IOException e) {
Log.e("Messages Server", e.toString());
}
}
}
这是logcat:
> 05-26 13:48:39.019: W/ActivityManager(1364): Timeout executing
> service: ServiceRecord{407b1838
> wifi/.auxiliar.Android_Wifi_Service} 05-26
> 13:48:39.179: I/Process(1364): Sending signal. PID: 4129 SIG: 3 05-26
> 13:48:39.179: I/dalvikvm(4129): threadid=4: reacting to signal 3 05-26
> 13:48:39.219: I/dalvikvm(4129): Wrote stack traces to
> '/data/anr/traces.txt' 05-26 13:48:39.229: I/Process(1364): Sending
> signal. PID: 1364 SIG: 3 05-26 13:48:39.229: I/dalvikvm(1364):
> threadid=4: reacting to signal 3 05-26 13:48:39.329: I/dalvikvm(1364):
> Wrote stack traces to '/data/anr/traces.txt' 05-26 13:48:39.339:
> I/Process(1364): Sending signal. PID: 1452 SIG: 3 05-26 13:48:39.339:
> I/dalvikvm(1452): threadid=4: reacting to signal 3 05-26 13:48:39.379:
> I/dalvikvm(1452): Wrote stack traces to '/data/anr/traces.txt' 05-26
> 13:48:39.389: I/Process(1364): Sending signal. PID: 1433 SIG: 3 05-26
> 13:48:39.389: I/dalvikvm(1433): threadid=4: reacting to signal 3 05-26
> 13:48:39.419: I/dalvikvm(1433): Wrote stack traces to
> '/data/anr/traces.txt' 05-26 13:48:39.919: D/dalvikvm(1364):
> GC_CONCURRENT freed 1086K, 42% free 5485K/9351K, external 1223K/1735K,
> paused 7ms+12ms 05-26 13:48:40.089: D/dalvikvm(1364): GC_EXPLICIT
> freed 116K, 41% free 5529K/9351K, external 1223K/1735K, paused 108ms
> 05-26 13:48:40.829: E/ActivityManager(1364): ANR in
> wifi:Service 05-26 13:48:40.829:
> E/ActivityManager(1364): Reason: Executing service
> wifi/.auxiliar.Android_Wifi_Service
任何人都可以帮助我吗?
答案 0 :(得分:8)
通过调用Thread
,您无法启动run()
。您可以致电Thread
开始start()
。现在,您只是在主应用程序线程上调用自己的Thread
run()
方法。