Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在message类中使用what字段?_Java_Android - Fatal编程技术网

Java 如何在message类中使用what字段?

Java 如何在message类中使用what字段?,java,android,Java,Android,有人能给我解释一下如何在what字段中使用开关来确定要执行的代码吗。 另外,如何创建要在交换机中使用的消息obj也很好 我的处理程序代码示例: Handler uiHandler = new Handler(){ public void handleMessage(Message msg){ switch(msg.what){ } } }; 如果您使用的是处理程序,我假设您希望在不同的线程中执行一些工作,并使用处理程序与正在启动的线程和主线程进

有人能给我解释一下如何在
what
字段中使用
开关来确定要执行的代码吗。
另外,如何创建要在交换机中使用的消息obj也很好

我的处理程序代码示例:

Handler uiHandler = new Handler(){
    public void handleMessage(Message msg){
        switch(msg.what){

        }
    }
};

如果您使用的是处理程序,我假设您希望在不同的线程中执行一些工作,并使用处理程序与正在启动的线程和主线程进行b/w通信。以以下为例:

private static final int SUCCESS = 0;
private static final int FAIL = 1;

//This is the handler
Handler uiHandler = new Handler(){
    @Override
    public void handleMessage(Message msg){
        //Here is how you use switch statement
        switch(msg.what){
        case SUCCESS:
            //Do something      
            break;
        case FAIL:
            //Do something
            break;
        }

    }
};

//Here is an example how you might call it
Thread t = new Thread() {
    @Override
    public void run(){
        doSomeWork();
        if(succeed){
            /*we can't update the UI from here so we'll signal our handler 
             and it will do it for us.*/
            // 'sendEmptyMessage(what)' sends a Message containing only the 'what' value.
            uiHandler.sendEmptyMessage(SUCCESS);
        }else{
            uiHandler.sendEmptyMessage(FAIL);
        }
    }   
}
这两条线索值得称赞:它们可能是一本很好的读物: &


希望这有帮助

如果您使用的是处理程序,我假设您希望在不同的线程中执行一些工作,并使用处理程序与正在启动的线程和主线程进行b/w通信。以以下为例:

private static final int SUCCESS = 0;
private static final int FAIL = 1;

//This is the handler
Handler uiHandler = new Handler(){
    @Override
    public void handleMessage(Message msg){
        //Here is how you use switch statement
        switch(msg.what){
        case SUCCESS:
            //Do something      
            break;
        case FAIL:
            //Do something
            break;
        }

    }
};

//Here is an example how you might call it
Thread t = new Thread() {
    @Override
    public void run(){
        doSomeWork();
        if(succeed){
            /*we can't update the UI from here so we'll signal our handler 
             and it will do it for us.*/
            // 'sendEmptyMessage(what)' sends a Message containing only the 'what' value.
            uiHandler.sendEmptyMessage(SUCCESS);
        }else{
            uiHandler.sendEmptyMessage(FAIL);
        }
    }   
}
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {             
        case SECOND_VALUE:
            String s = (String) msg.obj;  // if msg.obj is a string
            break;

        case FIRST_VALUE:
            AnotherObject = (AnotherObject) msg.obj;   // if it is another object
            break;

        default:
            super.handleMessage(msg);
        }
    }
这两条线索值得称赞:它们可能是一本很好的读物: &


希望这有帮助

非常有帮助,我想我也可以发送一个数据包,对吗?捆绑是最后的选择,首先使用arg0、arg1和obj。使用Handler.obtainMessage()或Message.get()初始化具有正确参数的消息。同样,处理程序不是用来和不同的线程对话的。OP说了什么,你也可以用它和你自己对话。非常有帮助。我想我也可以用它发送一个数据包。包是最后的选择,首先使用arg0、arg1和obj。使用Handler.obtainMessage()或Message.get()初始化具有正确参数的消息。同样,处理程序不是用来和不同的线程对话的,你也可以用它来和自己对话
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {             
        case SECOND_VALUE:
            String s = (String) msg.obj;  // if msg.obj is a string
            break;

        case FIRST_VALUE:
            AnotherObject = (AnotherObject) msg.obj;   // if it is another object
            break;

        default:
            super.handleMessage(msg);
        }
    }