Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一次在多个模拟器上发送sms_Java_Android - Fatal编程技术网

Java 一次在多个模拟器上发送sms

Java 一次在多个模拟器上发送sms,java,android,Java,Android,我正在开发一个Android应用程序,我需要发送一条消息给 一次接收多个模拟器。但问题只有一个 emulator正在接收消息。这是我的代码 公共类短信扩展活动 { 要向在同一台计算机上运行的另一个仿真程序实例发送SMS消息,请启动SMS应用程序。将目标仿真程序实例的控制台端口号(例如:5555)指定为SMS地址 请注意,此答案基于:guide。您可以创建仿真器id或号码的数组,然后将sendTextMessage放入数组中,执行数组中尽可能多的元素循环。或者您可以拥有允许用户将电话号码或仿真器

我正在开发一个Android应用程序,我需要发送一条消息给 一次接收多个模拟器。但问题只有一个 emulator正在接收消息。这是我的代码

公共类短信扩展活动 {


要向在同一台计算机上运行的另一个仿真程序实例发送SMS消息,请启动SMS应用程序。将目标仿真程序实例的控制台端口号(例如:5555)指定为SMS地址


请注意,此答案基于:guide。

您可以创建仿真器id或号码的数组,然后将sendTextMessage放入数组中,执行数组中尽可能多的元素循环。或者您可以拥有允许用户将电话号码或仿真器号码插入列表的UI,并执行与上述相同的过程!!! 请注意,sendTextMessage`s第一个参数是要向其发送短信的“电话号码” 只要在每次循环迭代时用您的需求替换它即可

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    btnSendSMS = (Button) findViewById(R.id.btn_SendSms);

    txtPhoneNo = (EditText) findViewById(R.id.edittext_PhoneNumber);

    txtMessage = (EditText) findViewById(R.id.edittext_MessageBody);

    btnSendSMS.setOnClickListener(new View.OnClickListener() 

    {
        public void onClick(View v) 

        {                
            String message = txtMessage.getText().toString();

            String phoneNo = txtPhoneNo.getText().toString();

            StringTokenizer st=new StringTokenizer(phoneNo,",");

            while (st.hasMoreElements())

            {
                String tempMobileNumber = (String)st.nextElement();

                if(tempMobileNumber.length()>0 && message.trim().length()>0)

                {
                    sendSMS(tempMobileNumber, message);
                }

                else 

                {
                    Toast.makeText(getBaseContext(), 

                            "Please enter both phone number and message.", 

                            Toast.LENGTH_SHORT).show();
                }
            }

        }
    });  

}


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



    SmsManager sms = SmsManager.getDefault();

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

}