在机器人编程方式发送短信发送短信、机器人、方式

2023-09-11 12:15:09 作者:白天不懂爷的黑

确定。我通过我的应用程序发送短信。后的文本消息被发送时,它发送一个状态更新到服务器。这部分工作好,但我遇到的问题是双重的。我不知道他们是否有关系,但我认为他们是。

OK. I am sending text messages through my app. After a text message is sent, it sends a status update to a server. This portion works okay, but the problem I am running into is twofold. I am not sure if they are related, but I assume that they are.

我的应用程序必须发送一个文本到多个用户的能力。这里是code样本...

My app has the ability to send a single text to multiple users. Here is a sample of the code...

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个以上的号码发送文本,有时文本不是所有实际上会发送。它发生很随意。

Basically, it just loops through an array of phone numbers, and sends the text one at a time. Here is where part of my issue lies. If I choose 3 or more numbers to send the text to, sometimes not all of the texts actually get sent. It happens very randomly.

我想这是因为在发送每封邮件之间的延迟,但code不会等待足够长的时间。我达到了这个假设,因为如果我踏进程序使用Eclipse和手工办理的程序,一切都总是工作得很好。

I assume it is because there is a delay between sending each individual message, but the code doesn't wait long enough. I reached this assumption because if I step into the program using eclipse and manually go through the app, everything always works just fine.

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

My other issue is when I send off the text message status update to a web server.

紧随短信被发送后,该应用程序,然后连接到互联网,并告诉通过HTTP服务器后已发送的文本的数量。这是我的互联网code片段...

Immediately after the text messages get sent, the app then connects to the internet and tells the server via an http post the number of texts that were sent. Here is my snippet of internet code...

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秒的连接超时。再次,正如我刚才所说,互联网连接,立即发送文本后会发生。所以,如果我进入调试模式,并通过流程手动步骤,一切工作正常。但是,如果我只是在我的手机上运行的应用程序,我会得到一个连接超时错误。

This section just compiles the items for the post, connects to a web page, and sends the post. The key here is the 10 second connection timeout. Once again, like I said earlier, the internet connection happens immediately after sending the texts. So, if I go into debug mode and manually step through the process, everything works fine. But if I just run the app on my phone, I will get a connection time out error.

现在,我希望如果我能减少短信的数量,以一个单一的文本,无论收件人的数量,这将是真棒。我曾尝试用逗号分隔的电话号码,并没有奏效。我也试过用分号分隔数字(酷似微​​软的Outlook,或Gmail是如何让您添加多个收件人的邮件)。这些都不适合我。有没有人有什么建议?即使有不同的方法完全,这将是AP preciated。哦,我不希望使用谷歌消息的意图发送消息,我需要用我自己的应用程序。

Now, I am hoping that if I can reduce the number of text messages to one single text, regardless of the number of recipients, that would be awesome. I have tried separating the phone numbers with a comma, and that didn't work. I also tried separating the numbers with a semi-colon (exactly like how Microsoft Outlook, or GMail lets you add multiple recipients to an email). None of those worked for me. Does anyone have any suggestions? Even if there is a different approach altogether, that would be appreciated. Oh, and I don't want to use the Google Messaging intent to send the messages, I need to use my own app.

推荐答案

您确实需要发送后previous一个下一个SMS发送,为此,你需要检查短信发送的状态,请的请参见本教程,它说:

You actually need to send the next sms after previous one is sent, for this you need to check the status of sms sent,Please see this tutorial, it says:

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