Android没有得到正确的布尔值

时间:2015-11-14 17:14:30

标签: java android sockets

我遇到了一些java逻辑的问题..

我正在创建一个使用Socket IO的应用程序,但我的布尔值有问题。

代码

MainActivity.class

package packname.shutdownpc;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;




public class MainActivity extends AppCompatActivity {


    private ImageView Switch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Controller controller = new Controller();
        controller.mSocket.on("state", controller.checkState);
        controller.mSocket.connect();


        Boolean isOn = controller.isOn(); //This isnt working
        Log.d("Main Activity Condition", Boolean.toString(isOn)); //This isnt Working

        Switch = (ImageView) findViewById(R.id.imageView);
        if(isOn) //This is always false because that isnt working..
            Switch.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.switch_green, null));
        else
            Switch.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.switch_red, null));


    }

}

Controller.class

package packname.shutdownpc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;


/**
 * Created by IvoMi on 14/11/2015.
 */
public class Controller extends Activity {

    public boolean isOn;


    public Socket mSocket; {
        try {
            mSocket = IO.socket("http://**.**.*.***:8080/");
        } catch (URISyntaxException e) {
            throw new RuntimeException();
        }
    }


    public Emitter.Listener checkState = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {

            JSONObject data = (JSONObject) args[0];
            String status;

            try {
                status = data.getString("status");

            } catch (JSONException e) {
                return;
            }

            Log.d("Text", status); //Its working
            if(status.equals("online")) { //This if is working
                isOn = true;
                Log.d("CONDIDITON", "ITS WORKING");
                Log.d("BOOLEAN", Boolean.toString(isOn));
            }


        }
    };


    public boolean isOn() {
        boolean isOnline;
        isOnline = isOn;

        return isOnline;
    }

}

所以,我知道我的socket我的工作..我做了一些调试来检查它,我会告诉他们

11-14 12:03:55.633 9991-10018/? D/Text: online
11-14 12:03:55.633 9991-10018/? D/CONDIDITON: ITS WORKING
11-14 12:03:55.633 9991-10018/? D/BOOLEAN: true

这是将“在线”发送到客户端的代码(server.js)

client.emit("state", {status: "online"});

我的问题是当我打开应用程序时需要显示绿色图像,因为当我通过套接字收到“在线”字符串时,我将isOn值设置为true。

但我做错了因为不工作,我希望你们明白这个问题,谢谢大家

1 个答案:

答案 0 :(得分:1)

检查是否存在竞争条件,其中在执行controller.isOn()之后调用checkState的调用方法。如果情况确实如此,您可能希望在MainActivity中设置一个侦听器,该侦听器最后由checkState的调用方法回调。

作为旁注,您确定要让Controller扩展活动吗?

Kaamel