如何仅在处理程序中将消息发送到特定处理程序

时间:2014-04-03 08:15:01

标签: android

如何在处理程序内发送消息???在我的应用程序这么多处理程序即时通讯使用数字假设如果我想发送消息到11号处理程序我会做什么?假设我想发送字符串" hello world"还有处理程序我会做什么????

                      try{
                               }



                     catch (Exception e) {

        android.os.Message alertMessage = new android.os.Message();
        alertMessage.what = 11;
        //i want to sent message"hello wrold" also what i do                                            
               handle.sendMessage(alertMessage);
                               }




                              Handler handle = new Handler() {
    public void handleMessage(android.os.Message msg) {
        super.handleMessage(msg);
                       if (msg.what == 11) {
            try {

               //show messgae"helloworld" here
                                     }

1 个答案:

答案 0 :(得分:4)

Message alertMessage = new android.os.Message();
alertMessage.what = 11;
alertMessage.obj = "test message"; //this is how you send an object (in this case a string)
handle.sendMessage(alertMessage);


//then in your handler
Handler handle = new Handler() {
    public void handleMessage(android.os.Message msg) {
        super.handleMessage(msg);
         if (msg.what == 11) {
             String text = (String)msg.obj; //here is your text message
         }

    }
}