gpt4 book ai didi

Android 服务被 ANR 杀死

转载 作者:行者123 更新时间:2023-11-29 14:33:26 25 4
gpt4 key购买 nike

我的应用程序中有一个服务(由一个线程创建),我在里面创建了另一个线程,它是一个 TCP 服务器。在启动线程(并完全工作、接收和处理收到的包)后不久,服务被“超时执行服务”和我的服务中的 ANR 终止

这是我的服务代码和执行所有工作的线程:

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

谁能帮帮我?

最佳答案

您不会通过调用 run() 来启动 Thread。您通过调用 start() 启动一个 Thread。现在,您只是在主应用程序线程上调用您自己的 Threadrun() 方法。

关于Android 服务被 ANR 杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com