等待进程结束,开始另一个进程 [英] wait for the process to finish to start another process

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

问题描述

我用一个按钮,当用户点击按钮,它执行两个功能,一是发送短信和另一种是发送电子邮件。
什么情况是,当我点击该按钮的消息越来越发送和突然的电子邮件客户端选择窗口弹出窗口。我想要的电子邮件客户端,选择窗口只完成了短信发送功能后显示。

什么,我有我的code键使为了得到良好的输出编辑。

 按钮HI =(按钮)findViewById(R.id.button1);
     hi.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            sendsms();
            发电子邮件();
     }     私人无效sendemail(){        意图电子邮件=新意图(Intent.ACTION_SEND);
        email.putExtra(Intent.EXTRA_EMAIL,新的String [] {TO1});
        email.putExtra(Intent.EXTRA_CC,新的String [] {TO2});
        email.putExtra(Intent.EXTRA_BCC,新的String [] {TO3});
        email.putExtra(Intent.EXTRA_BCC,新的String [] {TO4});
        email.putExtra(Intent.EXTRA_BCC,新的String [] {到5});        email.putExtra(Intent.EXTRA_SUBJECT,学科);
        email.putExtra(Intent.EXTRA_TEXT,emailmessage);        //需要这个提示仅电子邮件客户端
        email.setType(信息/ RFC822);        startActivity(Intent.createChooser(电子邮件,选择电子邮件客户端));
    }
    sendsms()
    {
        串receipentsNumber [] = {111,222,333,444,555};        的for(int i = 0; I< receipentsNumber.length;我++){            尝试{
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(receipentsNumber [I],空消息,空,
                        空值);
                System.ot.println(getApplicationContext(),短信发送到++ receipentsNumber [I],Toast.LENGTH_LONG).show();            }赶上(例外五){
                Toast.makeText(getApplicationContext(),
                        短信faild,请稍后重试!,Toast.LENGTH_LONG)
                        。显示();
                e.printStackTrace();
            }        }
    }});


解决方案

根据您的code,你必须等待SMS状态,或者我们可以说,成功的或失败的回拨。你可以使用广播接收机,以处理事件,并返回一些反馈。

你还可以短信的成功后,打电话给你的电子邮件的功能。

检查参考例这是您的最佳选择。

如果您需要监控短信发送过程的状态,你可以实际使用两个待定意向有两个广播接收器的对象,像这样一起对象:
一个用于发送短信和放大器;其次为交货状态。

  // ---发送短信到另一台设备---
  私人无效sendSMS(字符串phoneNumber的,字符串消息){
      字符串SENT =SMS_SENT;
      字符串DELIVERED =SMS_DELIVERED;      的PendingIntent sentPI = PendingIntent.getBroadcast(这一点,0,
      新意图(SENT),0);      的PendingIntent deliveredPI = PendingIntent.getBroadcast(这一点,0,
      新意图(交付),0);      // ---当短信已发送---
      registerReceiver(新广播接收器(){@覆盖
          公共无效的onReceive(上下文为arg0,ARG1意向){
              开关(的getResult code()){
                  案例Activity.RESULT_OK:
                      Toast.makeText(getBaseContext(),短信发送
                      Toast.LENGTH_SHORT).show();
                      打破;
                  案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                      Toast.makeText(getBaseContext(),一般故障
                      Toast.LENGTH_SHORT).show();
                      打破;
                  案例SmsManager.RESULT_ERROR_NO_SERVICE:
                      Toast.makeText(getBaseContext(),无服务,
                      Toast.LENGTH_SHORT).show();
                      打破;
                  案例SmsManager.RESULT_ERROR_NULL_PDU:
                      Toast.makeText(getBaseContext(),空的PDU,
                      Toast.LENGTH_SHORT).show();
                      打破;
                  案例SmsManager.RESULT_ERROR_RADIO_OFF:
                      Toast.makeText(getBaseContext(),无线电关
                      Toast.LENGTH_SHORT).show();
                      打破;
              }
          }
      },新的IntentFilter(SENT));      // ---当短信已交付---
      registerReceiver(新广播接收器(){@覆盖
          公共无效的onReceive(上下文为arg0,ARG1意向){
              开关(的getResult code()){
                  案例Activity.RESULT_OK:
                      Toast.makeText(getBaseContext(),短信发送
                      Toast.LENGTH_SHORT).show();
                      打破;
                  案例Activity.RESULT_CANCELED:
                      Toast.makeText(getBaseContext(),短信未送达
                      Toast.LENGTH_SHORT).show();
                      打破;
              }
          }
      },新的IntentFilter(送到));      SmsManager短信= SmsManager.getDefault();
      sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);
  }

i am used a button, when user clicks on the button it perform two functions, one sending sms and other one is sending email. What happening is that when i click on this button messages are getting send 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.

What the edit that i have to make in my code in order to get a good output.

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

        }
    }

});

解决方案

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.

Check Reference Example which is perfect for you.

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天全站免登陆