如何防止应用程序超时?

时间:2009-03-17 02:58:15

标签: android multithreading android-service

我创建了一个多线程应用程序,但如果服务器不可用,它仍会挂起。

在主Activity中我创建了以下方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //bind GUI elements
    ...

    //start TCPConnection Service in a new thread
    establishTCPConnection();
    ...

}


private void establishTCPConnection(){
    TCPClientServiceThread = new Thread(null, backgroundConnection, "connection");
    TCPClientServiceThread.start();
}

..


private Runnable backgroundConnection = new Runnable(){
    public void run(){
        //establish TCP connection
        doEstablishTCPConnection();
    }
};

private void doEstablishTCPConnection()
{
    //start TCPConnection service
    startService(new Intent(this, TCPClientService.class));
}

这是TCPClientService类:

public class TCPClientService extends Service{

...

private String serverAddress = "192.168.1.5";
private int portNumber = 1000;



@Override
public void onCreate()
{
    //TODO: Actions to perform when service is created
    connectionAvailable = false;

    IntentFilter dataReceiverFilter;
    dataReceiverFilter = new IntentFilter(MotranetClient.MOTION_DATA_UPDATED);
    dataReceiver = new DataReceiver();
    registerReceiver(dataReceiver, dataReceiverFilter);

    EstablishConnection();
}

@Override
public IBinder onBind(Intent intent)
{
    //TODO: Replace with service binding implementation
    return null;
}

private void EstablishConnection()
{
    try {
           InetAddress serverAddr = InetAddress.getByName(serverAddress);
           Log.d("TCP", "C: Connecting...");
           Socket socket = new Socket(serverAddr, portNumber);
           String message = "testing connection";

               try {
                Log.d("TCP", "C: Sending: '" + message + "'");
                PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
                out.println(message);
                Log.d("TCP", "C: Sent.");
                  Log.d("TCP", "C: Done.");
                  connectionAvailable = true;

             } catch(Exception e) {
                 Log.e("TCP", "S: Error", e);
                 connectionAvailable = false;

               } finally {
                  socket.close();
                  announceNetworkAvailability(connectionAvailable);
                }

         } catch (Exception e) {
              Log.e("TCP", "C: Error", e);
              announceNetworkAvailability(connectionAvailable);
         }
    }


}

当服务器不可用时,行

Socket socket = new Socket(serverAddr, portNumber);

导致一些延迟,我相信这是悬挂的原因。但是如果TCPClientService服务在自己的线程中执行,我不知道为什么这会影响主要活动的超时。

如果有人能说明如何在服务器不可用时阻止应用程序挂起,我将非常感激。

1 个答案:

答案 0 :(得分:2)

Android平台-always-在主要事件线程中运行Activitys和Services的生命周期方法。所以你创建另一个线程,并从中调用startService ...但是当创建并启动Service时,将从主事件线程调用onCreate()etc方法,而不是来自用户线程。

执行此操作的正确方法不是从另一个线程调用startService,而是正常调用startService,然后让服务从onCreate()中生成一个连接线程。

ssakl的解决方案并没有解决我担心的基本问题。