等待进程完成以启动另一个进程

时间:2014-01-11 04:26:50

标签: android function email sms

当用户点击我的按钮时,它会执行两项功能:发送短信和发送电子邮件。

当我点击此按钮时,正在发送短信,并且电子邮件突然选择客户端窗口弹出窗口。我希望电子邮件客户端选择器窗口仅在完成SMS发送功能后显示。

我应该如何更改我的代码?

     Button hi= (Button) findViewById(R.id.button1);
     hi.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            sendsms();
            sendemail();
     } 

     private void sendemail() {

        Intent email = new Intent(Intent.ACTION_SEND);
        email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to1});
        email.putExtra(Intent.EXTRA_CC, new String[]{ to2});
        email.putExtra(Intent.EXTRA_BCC, new String[]{to3});
        email.putExtra(Intent.EXTRA_BCC, new String[]{to4});
        email.putExtra(Intent.EXTRA_BCC, new String[]{to5});

        email.putExtra(Intent.EXTRA_SUBJECT, subject);
        email.putExtra(Intent.EXTRA_TEXT, emailmessage);

        //need this to prompts email client only
        email.setType("message/rfc822");

        startActivity(Intent.createChooser(email, "Choose an Email client :"));


    }


    sendsms()
    {
        String receipentsNumber[] = {"111","222","333","444","555"};

        for (int i = 0; i < receipentsNumber.length; i++) {

            try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(receipentsNumber[i], null, message, null,
                        null);
                System.ot.println(getApplicationContext(), "SMS Sent to" + " " + receipentsNumber[i], Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                        "SMS faild, please try again later!", Toast.LENGTH_LONG)
                        .show();
                e.printStackTrace();
            }

        }
    }

});

1 个答案:

答案 0 :(得分:6)

根据您的代码,您必须等待SMS状态,否则我们可以说成功或失败回叫。您可以使用Broadcast Receiver来处理EVENT并返回一些反馈。

您可以在短信成功后拨打电子邮件功能。

Check Reference Example这对你来说很完美。

如果您需要监控SMS消息发送过程的状态,您实际上可以使用两个Pending Intent对象和两个BroadcastReceiver对象,如下所示: 一个用于发送短信&amp;第二个是交付状态。

  //---sends an SMS message to another device---
  private void sendSMS(String phoneNumber, String message) {
      String SENT = "SMS_SENT";
      String DELIVERED = "SMS_DELIVERED";

      PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
      new Intent(SENT), 0);

      PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
      new Intent(DELIVERED), 0);

      //---when the SMS has been sent---
      registerReceiver(new BroadcastReceiver() {@Override
          public void onReceive(Context arg0, Intent arg1) {
              switch (getResultCode()) {
                  case Activity.RESULT_OK:
                      Toast.makeText(getBaseContext(), "SMS sent",
                      Toast.LENGTH_SHORT).show();
                      break;
                  case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                      Toast.makeText(getBaseContext(), "Generic failure",
                      Toast.LENGTH_SHORT).show();
                      break;
                  case SmsManager.RESULT_ERROR_NO_SERVICE:
                      Toast.makeText(getBaseContext(), "No service",
                      Toast.LENGTH_SHORT).show();
                      break;
                  case SmsManager.RESULT_ERROR_NULL_PDU:
                      Toast.makeText(getBaseContext(), "Null PDU",
                      Toast.LENGTH_SHORT).show();
                      break;
                  case SmsManager.RESULT_ERROR_RADIO_OFF:
                      Toast.makeText(getBaseContext(), "Radio off",
                      Toast.LENGTH_SHORT).show();
                      break;
              }
          }
      }, new IntentFilter(SENT));

      //---when the SMS has been delivered---
      registerReceiver(new BroadcastReceiver() {@Override
          public void onReceive(Context arg0, Intent arg1) {
              switch (getResultCode()) {
                  case Activity.RESULT_OK:
                      Toast.makeText(getBaseContext(), "SMS delivered",
                      Toast.LENGTH_SHORT).show();
                      break;
                  case Activity.RESULT_CANCELED:
                      Toast.makeText(getBaseContext(), "SMS not delivered",
                      Toast.LENGTH_SHORT).show();
                      break;
              }
          }
      }, new IntentFilter(DELIVERED));

      SmsManager sms = SmsManager.getDefault();
      sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
  }