Android系统在服务中杀死我的serversocket

时间:2011-04-16 07:22:08

标签: android system serversocket wakelock

我正在编写一个示例应用程序,用于在Android上创建服务器,以及用于连接到PC的客户端。我把serversocket放在服务的一个线程中。一切都很顺利,直到屏幕熄灭几分钟后。这可能是Android杀了我的服务器,我试图把一个完整的唤醒锁定到我的代码,它不会再杀了,但是,我想让屏幕像往常一样关闭。 这是我的代码:

public class MessageListener extends Service {

    private ServerSocket serverSocket;

    @Override
    public void onCreate() {
        Log.v("Test", "Create service");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


         PowerManager.WakeLock wl=null;
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
         wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
         wl.acquire();
        startServer();
        if(wl!=null) wl.release();

        return START_STICKY;
    }

    private Runnable thread = new Runnable() {

        @Override
        public synchronized void run() {
            try {



                serverSocket = new ServerSocket(Integer.parseInt(5000));
                ObjectInputStream in = null;

                while (true) {

                    Socket client = serverSocket.accept();

                    Log.v("TCP", "S: Receiving...");
                    try {
                        in = new ObjectInputStream(client.getInputStream());

                        DataInController data = new DataInController(
                                getApplicationContext());
                        data.processDataIn(in.readObject(), client);

                    } catch (ClassNotFoundException e) {
                        System.out.println("TCP S: Error in PC Server Listener");
                        e.printStackTrace();

                    } finally {
                         client.close();    
                    }
                }

            } catch (IOException e) {
            }

        }

    };
    private Thread serverThread;

    private synchronized void startServer() {
        if (serverThread == null) {
            serverThread = new Thread(thread);
            serverThread.start();
        }
    }

    private synchronized void stopServer() {
        if(serverThread!=null){
            Thread t=serverThread;
            serverThread=null;
            t.interrupt();
        }

    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v("TCP", "Killing Service!!!!!!!!!!!!!!!!!!!!!!!");
        if (serverSocket != null) {
            try {
                serverSocket.close();
                stopServer();
                Log.v("TCP", "Closed server socket");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

3 个答案:

答案 0 :(得分:3)

对我们有用的两件事:

  • Wi-Fi lock
  • 将Wi-Fi睡眠策略设置为never。有些设备会在没有此设置的情况下关闭Wi-Fi无线电,即使程序锁定Wi-Fi无线电也是如此。

答案 1 :(得分:0)

我发现了问题。那是路由器丢失了与Android的连接。我试过ping它并说它“无法访问”,重新连接到wifi后,它可以工作,但过了一段时间,它又来了

答案 2 :(得分:0)

同时尝试保留WakeLock。两者都适合我。