在Android是它可以发送短信给多个收件人code

在Android是它可以发送短信给多个收件人code

本文介绍了在Android是它可以发送短信给多个收件人code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 SmsManager的类,我可以发送相同的短信到多个号码,在同一时间?或者,我需要做一些循环?

Using the smsManager class can I send the same SMS to multiple numbers at the same time? Or would I need to do some sort of loop?

我想从我的应用程序不是通过消息传递程序发送短信。

I want to send the sms from within my app not through the messaging program.

推荐答案

下面我附上源$ C ​​$ C我做了以编程方式发送邮件。

Here i am attaching source code what i have done to send message programatically.

短信:

public class SMS extends Activity {

 Button btnSendSMS;
 EditText txtPhoneNo;
 EditText txtMessage;

/** 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.btnSendSMS);
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
    txtMessage = (EditText) findViewById(R.id.txtMessage);

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

//                if (phoneNo.length()>0 && message.length()>0)
//                    sendSMS(phoneNo, 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));

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

}

SmsReceiver:

SmsReceiver:

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i=0; i<msgs.length; i++)
        {
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            str += "SMS from " + msgs[i].getOriginatingAddress();
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }
}
}

和需要的清单设置权限READ_SMS,RECIEVE_SMS。这就是你可以发送消息给单个用户。

and need to set permission in manifest toREAD_SMS,RECIEVE_SMS.This is how you can send message to single user..

这是怎么ü可以发送消息给多个用户。...........................................假设u需要发送消息给多个用户U可以写一些以逗号分隔对于如。 5558,5554。

This is how u can send message to multiple user............................................Suppose u need to send message to multiple user u can write number separated by commafor eg. 5558,5554.

这是怎么ü可以单独用逗号隔开的两个数字用在上述code使用的字符串标记生成器类

This is how u can separate two numbers separated by comma usingString tokenizer class used in the above code

您可以套用这个逻辑,其中u呼吁SendSms功能,每一次通过appropiate电话号码

You can apply this logic where u are calling SendSms Function and pass appropiate phone number each time

它工作正常的me..u可以试一下..难道是回答你的问题?

It works fine for me..u can try it out..Is it answer to your question?

谢谢

拉​​克什

这篇关于在Android是它可以发送短信给多个收件人code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:24