如何在一个处理程序中获取多个消息

时间:2013-12-18 07:55:36

标签: android

我有2条消息被发送到处理程序mHandler()。我有msg.what =0msg.what =1。我需要检索消息及其对象,以便我可以获取我的图像和用户名。请帮忙

    @Override
    public void onComplete(final JSONObject response, Object state) {
                    // TODO Auto-generated method stub
                    Message msg = new Message();
                    msg.obj = response;
                    msg.what = 0;
                    mHandler.sendMessage(msg);
                    new Thread(){
                        @Override
                        public void run() {
                            if(response.has("figureurl")){
                                Bitmap bitmap = null;
                                try {
                                    bitmap = Util.getbitmap(response.getString("figureurl_qq_2"));
                                } catch (JSONException e) {

                                }
                                Message msg = new Message();
                                msg.obj = bitmap;
                                msg.what = 1;
                                mHandler.sendMessage(msg);
                            }
                        }

                    }.start();
                }
            };
              mTencent.requestAsync(Constants.GRAPH_SIMPLE_USER_INFO, null,
                        Constants.HTTP_GET, requestListener, null);
        } else {
            //mUserInfo.setText("");
            //mUserInfo.setVisibility(android.view.View.GONE);
            //mUserLogo.setVisibility(android.view.View.GONE);

            Intent intent = new Intent(LoginActivity.this, LeftAndRightActivity.class);
            intent.putExtra(USER_NAME, "");

            startActivity(intent);
        }
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            System.out.println(msg.what);
            if (msg.what == 0) {
                JSONObject response = (JSONObject) msg.obj;
                if (response.has("nickname")) {
                    try {
                        Intent i = new Intent(LoginActivity.this, LeftAndRightActivity.class);
                        i.putExtra(USER_NAME, response.getString("nickname"));
                        startActivity(i);


                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }if(msg.what == 1){
                Intent i = new Intent(LoginActivity.this, LeftAndRightActivity.class);
                Bitmap b = (Bitmap)msg.obj;
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.PNG, 50, bs);
                i.putExtra(BYTE_ARRAY, bs.toByteArray());
                startActivity(i);
            }
        }

    };

0 个答案:

没有答案
相关问题