当应用程序暂停或停止时,为什么服务中的套接字变为空?

时间:2017-01-26 22:07:15

标签: java android sockets

我有一个简单的应用程序,通过服务使用套接字连接进行p2p通信。据我所知,当应用程序被销毁时,服务将继续运行并需要停止。但是,当我的应用程序暂停或停止时(即活动不可见),服务器中的套接字变为空,应用程序崩溃。

以下是我的活动中启动服务的方法。

public void startServerService(){

    //if (!serverThreadActive) {
        serverServiceIntent = new Intent(this, ServerService.class);

        //serverServiceIntent.putExtra("GoAddress", goAddress);
        serverServiceIntent.putExtra("GOPort", prefixedPort);
        serverServiceIntent.putExtra("Result", new ResultReceiver(null){

            @Override
            protected void onReceiveResult(int resultCode, final Bundle resultData) {

                //String message = resultData.getString("msg");
                //addLine(message);   //back from ServerService with mssg so add to textview line 181
                final TextView tv = (TextView) findViewById(R.id.rcvd);
                tv.post(new Runnable() {
                    @Override
                    public void run() {
                        tv.append("\n" + (String)resultData.get("msg"));
                    }
                });

            }
        });

        serverThreadActive = true;
        startService(serverServiceIntent);
}

这是服务本身。

   public class ServerService extends IntentService{

    //DECLARATIONS
    private boolean serviceEnabled;
    private int mGoPort = 8080;
    private String message;
    private ResultReceiver serverResult;
    private InetAddress mGoAddress;
    private ServerSocket mServerSocket;
    private Thread mServerThread;


    public ServerService(){
        super("ServerService");
        serviceEnabled =  true;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        android.os.Debug.waitForDebugger();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //mGoPort = (Integer) intent.getExtras().get("GOPort");
        //mGoAddress = (InetAddress)intent.getExtras().get("GOAddress");
        serverResult = (ResultReceiver)intent.getExtras().get("Result");

        mServerThread = new Thread(new ServerThread());
        mServerThread.start();
    }

    class ServerThread implements Runnable{

        @Override
        public void run() {

            Socket socket;

            try {
                mServerSocket = new ServerSocket(mGoPort);
            } catch (IOException e) {
                e.printStackTrace();
            }

            while (!Thread.currentThread().isInterrupted()){

                try {
                    socket = mServerSocket.accept();            //THIS SOCKET IS NULL AFTER APP
                                                                //STOP AND RESTART
                    CommunicationThread commThread = new CommunicationThread(socket);
                    new Thread(commThread).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class CommunicationThread implements Runnable{

        private Socket clientSocket;
        private BufferedReader input;

        public CommunicationThread(Socket clientSocket){
            this.clientSocket = clientSocket;

            try {
                this.input = new BufferedReader(new InputStreamReader
                        (this.clientSocket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {

            while (!Thread.currentThread().isInterrupted()){

                try {
                    String read = input.readLine();
                    updateMessages(read, false);
                    //input.close();                  //TRY TO FIX CRASH
                    //clientSocket.close();           //ADDED TO TRY TO FIX CRASH
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public synchronized void updateMessages(String msg, boolean local){

        android.os.Debug.waitForDebugger();
        if (local) {
            msg = "me: " + msg;
        }
        else{
            msg = "them: " + msg;
        }

        Bundle messageBundle = new Bundle();
        messageBundle.putString("msg", msg);

        serverResult.send(mGoPort, messageBundle);
    }
}

当应用暂停或停止时,例如按下设备上的主页按钮时,变量mServerSocket变为空

socket = mServerSocket.accept();            //THIS SOCKET IS NULL AFTER APP
                                            //STOPS AND RESTARTS

如何解决这个问题,以便通讯能力在完成之后仍能正常运作 活动暂停并重新启动或停止并重新启动?

感谢您提出任何想法,提示或线索。

哦,这是清单部分

<service android:name=".ServerService"
    android:enabled="true"
    android:exported="false"/>

另一个更新 - 即使在onHandleIntent中等待的套接字在主活动暂停后仍然变为空。

0 个答案:

没有答案