在android中以编程方式发送短信

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

我的应用程序可以将单个文本发送给多个用户。 这是一个代码示例…

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

基本上,它只是通过一系列电话号码循环,并一次发送一个文本。 这是我的问题的一部分。 如果我select3个或更多的号码发送文本,有时并不是所有的文本都会被发送。 它非常随机地发生。

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

我的另一个问题是当我发送文本消息状态更新到Web服务器。

紧接着文本消息发送之后,应用程序将连接到互联网,并通过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(); 

本节仅编译post的项目,连接到网页,并发送post。 这里的关键是10秒连接超时。 再一次,就像我刚才所说的,互联网连接发生后立即发生的文字。 所以,如果我进入debugging模式并手动执行整个过程,一切正常。 但是,如果我只是在我的手机上运行应用程序,我会得到一个连接超时错误。

现在,我希望,如果我可以减less文本信息的数量到一个单一的文本,无论收件人的数量,这将是非常棒的。 我曾尝试用逗号分隔电话号码,这是行不通的。 我也尝试用分号分隔数字(就像Microsoft Outlook,或GMail可以让您将多个收件人添加到电子邮件中一样)。 这些都没有为我工作。 有没有人有什么build议? 即使有完全不同的方法,这将不胜感激。 哦,我不想用Google Messaging的意图发送消息,我需要使用我自己的应用程序。

您实际上需要在发送前一个短信之后发送下一个短信,为此您需要检查发送的短信的状态,请参阅本教程 ,其中说:

如果您需要监视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); } 

我想这是你需要的。 下面的代码片段提供了input长消息并将其分成部分,并将它们分别发送给给定的联系人或者甚至是一组联系人

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

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

你可以试试这个

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

谢谢