Handler发送消息携带多个参数

 通过Bundle携带多个参数进行发送

Message msg = new Message();
      msg.what = 2;
      Bundle bundle = new Bundle();
      bundle.putString("content", content);
      bundle.putString("button", "开始统计");
      msg.setData(bundle);
      mHandler.sendMessage(msg);

 

处理

private Handler mHandler = new Handler() {
  public void handleMessage(Message msg) {
   switch (msg.what) {
   case 1:
    btToDFModel.setText("");
    Toast.makeText(getApplicationContext(), (CharSequence) msg.obj,
      1).show();
    break;

   case 2:
    Bundle bundle = msg.getData();
    String content = bundle.getString("content");
    String button = bundle.getString("button");
    textView.setText(content);
    btCount.setText(button);
   default:
    break;
   }
   super.handleMessage(msg);
  }
 };

 

你可能感兴趣的:(Handler发送消息携带多个参数)