【问题标题】:Sending text messages programmatically in android在android中以编程方式发送短信
【发布时间】:2012-01-24 14:35:22
【问题描述】:

好的。我正在通过我的应用程序发送短信。发送短信后,它会向服务器发送状态更新。这部分工作正常,但我遇到的问题是双重的。我不确定它们是否相关,但我认为它们是相关的。

我的应用能够向多个用户发送单个文本。这是代码示例...

if(phoneNumbers.length > 0 && message.getText().toString().equals("") == false)
{
    for(int i=0;i<phoneNumbers.length;i++)
    {
        sms = SmsManager.getDefault();
        try
        {
            sms.sendTextMessage(phoneNumbers[i], null, message.getText().toString(), null, null);
            sentQuantity++;
        }
        catch(IllegalArgumentException e)
        {

        }
    }
}

基本上,它只是遍历一组电话号码,并一次发送一个文本。这是我的部分问题所在。如果我选择 3 个或更多数字来发送文本,有时并非所有文本都实际发送。它发生得非常随机。

我认为这是因为发送每条消息之间存在延迟,但代码等待的时间不够长。我达到了这个假设,因为如果我使用 eclipse 进入程序并手动浏览应用程序,一切都会正常工作。

我的另一个问题是当我将短信状态更新发送到网络服务器时。

在发送短信后,该应用会立即连接到互联网,并通过 http 帖子告诉服务器发送的短信数量。这是我的网络代码sn-p...

for(int i = 0; i < postNames.length; i++)
{
    nameValuePairs.add(new BasicNameValuePair(postNames[i], postValues[i]));
    }

    //http post
    try{

            HttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 10000;

            HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection );

            HttpClient httpclient = new DefaultHttpClient(httpParameters);              
            HttpPost httppost = new HttpPost(webAddress);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

此部分仅编译帖子的项目,连接到网页并发送帖子。这里的关键是 10 秒的连接超时。再一次,就像我之前说的,互联网连接在发送文本后立即发生。因此,如果我进入调试模式并手动逐步完成该过程,一切正常。但如果我只是在手机上运行该应用程序,我会收到连接超时错误。

现在,我希望如果我可以将短信数量减少到一个文本,而不管收件人的数量是多少,那就太棒了。我试过用逗号分隔电话号码,但没有奏效。我还尝试用分号分隔数字(就像 Microsoft Outlook 或 GMail 允许您将多个收件人添加到电子邮件中一样)。这些都不适合我。有没有人有什么建议?即使有完全不同的方法,也将不胜感激。哦,我不想使用 Google 消息传递意图来发送消息,我需要使用自己的应用程序。

【问题讨论】:

  • 仅供参考,电话中的逗号是暂停,而不是分隔符。例如,如果您拨打一个号码并且在输入分机之前必须等待,您可以写 2135551212,,,,123 这将呼叫该号码,等待 5 次暂停,然后再拨打 123。

标签: android sms android-intent


【解决方案1】:

实际上你需要在上一条发送之后再发送下一条,为此你需要检查发送的短信的状态,请see this tutorial,上面写着:

如果需要监控短信发送过程的状态,其实可以使用两个PendingIntent对象和两个BroadcastReceiver对象,像这样

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

【讨论】:

  • 这不能回答我的问题。问题的简短版本是,“如何将我的多条短信合并成一条有多个收件人的短信?”
  • 实际上需要在发送上一条之前发送下一条短信
  • @YaqubAhmad 感谢您的回答。不过,我还发现,当我们完成广播时,有必要在 OnRecieve() 回调中调用 unregisterReceiver(this);,否则我们最终会得到泄露的意图接收器。
  • 为什么这个答案被接受了? “您实际上需要在发送前一条短信之前发送下一条短信”没有意义,您将如何在发送第一条短信之前发送第二条短信......人们只是投票是因为它提供了一些有用的代码,即使它没有回答问题?还是我只是错过了什么?如果是这样,请原谅...
  • @LucasBailey 如果我理解答案,这意味着您实际上无法将一条短信发送给多个收件人。对吗?
【解决方案2】:

我想这就是你需要的。 下面的 sn-p 提供了输入长消息并将它们分成部分并将每个消息单独发送给给定联系人甚至一组联系人

public void sendLongSMS() {

    String phoneNumber = "0123456789";
    String message = "Hello World! Now we are going to demonstrate " + 
        "how to send a message with more than 160 characters from your Android application.";

    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<String> parts = smsManager.divideMessage(message); 
    smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
}

【讨论】:

  • 这没有回答所提出的问题,即如何将相同的消息发送到多个号码,而不是将多部分消息发送到单个号码。
  • 谢谢兄弟,你拯救了我的一天兄弟。
【解决方案3】:

首先你必须在 AndroidManifest.xml 文件中添加权限

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

然后写这段代码

try {

                String ph="1234568790";
                String msg="Haiii friend";

                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(ph, null,msg, null, null);
                Toast.makeText(MainActivity.this, "Message Sent",
                        Toast.LENGTH_LONG).show();
            }
            catch (Exception e)
            {
                Toast.makeText(MainActivity.this, "Message not Sent",
                        Toast.LENGTH_LONG).show();
            }

【讨论】:

  • 这没有回答所提出的问题,即如何将消息发送到多个号码,而不是像此代码(与原始问题中的代码基本相同)那样只发送一个号码。
【解决方案4】:

你可以试试这个

SmsManager.getDefault().sendTextMessage(phoneNUmber, null, messageToSend, null, null);

谢谢

【讨论】:

  • 这并没有回答所提出的问题,即如何将消息发送到多个号码,而不是像此代码(与原始问题中的代码基本相同)那样只发送一个号码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多