等待进程完成以启动另一个进程 [英] wait for the process to finish to start another process

查看:185
本文介绍了等待进程完成以启动另一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When the user clicks on my button, it performs two functions: sending an SMS and sending an email.

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

When I click on this button, the SMS is being sent and suddenly the email choose client window pop ups. I want the email client chooser window to show only after completing the SMS sending function.

如何更改代码?

     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();
            }

        }
    }

});


推荐答案

根据你的代码,你必须等待短信状态或我们可以说成功或失败回叫。您可以使用将处理EVENT并返回一些反馈的广播接收器

As per your code you have to wait for the SMS status or we can say Success or Failed Call back. You can do using Broadcast Receiver which will handle EVENT and return some feedback.

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

And you can call your email function after success of SMS.

检查参考示例,这是完美的。

如果您需要监控短信发送过程的状态,您实际上可以使用两个预定意图对象以及两个 BroadcastReceiver 对象,如下所示:
发送SMS&

If you need to monitor the status of the SMS message sending process, you can actually use two Pending Intent objects together with two BroadcastReceiver objects, like this: One for Sent SMS & Second for that Delivery status.

  //---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);
  } 

这篇关于等待进程完成以启动另一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆