在android中以编程方式发送文本消息

时间:2011-12-20 16:31:47

标签: android sms android-intent

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

我的应用可以向多个用户发送单个文字。以下是代码示例...

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帖子告诉服务器发送的文本数量。这是我的互联网代码片段......

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 Messaging意图发送消息,我需要使用自己的应用程序。

4 个答案:

答案 0 :(得分:71)

您实际上需要在发送上一个短信后发送下一个短信,为此您需要检查发送的短信的状态,请see this tutorial,它说:

如果您需要监控SMS消息发送过程的状态,您实际上可以使用两个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);        
    }

答案 1 :(得分:18)

我想这就是你需要的。 下面的代码片段用于输入长消息并将它们分成一部分并将其中的每一个单独发送给给定的联系人或甚至是一组联系人

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

答案 2 :(得分:1)

首先,您必须在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();
            }

答案 3 :(得分:-1)

你可以试试这个

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

由于