0

我不完全确定如何解释这一点,但我是 AsyncTask 的新手,我想做的是向我的服务器发送一个 UDP 数据包,如果我得到回复,如果不是离线,它就会将其设置为在线。现在它在第一次打开应用程序时可以正常工作,但是一旦加载了新页面,它就会说服务器处于离线状态,即使它仍然在线。我不确定它有什么问题。我是在错误地执行 AsyncTask 还是到了那种程度?这是我唯一能想到的。

检查状态.java

import java.io.IOException;
import java.net.*;

public class CheckStatus {
//Check if the server is online

    public static boolean check() {

        try {
            byte[] receiveData = new byte[1024];
            InetAddress address = InetAddress.getByName("11.11.11.11");
            //create socket
            DatagramSocket clientSocket = new DatagramSocket();
            //set timeout
            clientSocket.setSoTimeout(1000);
            //send packet
            DatagramPacket p = new DatagramPacket(Integer.toBinaryString(0x0006000000).getBytes(), 5, address, 44462);
                .send(p);
            //try to receive packet from server
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            clientSocket.close();
            return true;
        } catch (Exception e) {
            //if nothing is received set the the server status offline
            return false;
        }
    }   
}

CheckStatusTask.java

import android.os.AsyncTask;

class CheckStatusTask extends AsyncTask<Object, Object, Boolean> { 

    private static boolean online;

    protected Boolean doInBackground(Object... arg0) {
        boolean flag = CheckStatus.check();
        return flag;
    }

    protected void onPostExecute(Boolean flag) {
        online = flag;
    }

    public static boolean getOnline(){
        return online;
    }
}

main.java

import android.os.Bundle;
import android.view.View;

import com.swgproject.projectswg.ActionBar;

public class Main extends ActionBar {

//Include res/layout/main.xml
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    View background = findViewById(R.id.status);
    View status = findViewById(R.id.image);
    new CheckStatusTask().execute();
    if(CheckStatusTask.getOnline()){
        background.setBackgroundResource(R.layout.offline);
        status.setBackgroundResource(R.drawable.offline);
    }
}


}

非常感谢任何帮助谢谢

4

1 回答 1

1

现在你的问题是你没有正确访问结果。您要求任务执行,然后在不知道任务完成与否的情况下立即访问结果。我建议让您的异步任务成为这样的内部类,以便它可以访问以下变量Main

主.java

public class Main extends ActionBar {
    private View background;
    private View status;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            background = findViewById(R.id.status);
            status = findViewById(R.id.image);
            new CheckStatusTask().execute();
        }

        class CheckStatusTask extends AsyncTask<Object, Object, Boolean> { 

            protected Boolean doInBackground(Object... arg0) {
                return CheckStatus.check();
            }

            protected void onPostExecute(Boolean flag) {
                if(flag){
                        background.setBackgroundResource(R.layout.offline);
                        status.setBackgroundResource(R.drawable.offline);
                }
            }

        }

}
于 2013-01-22T04:00:21.737 回答