无法在Android中使用SMSManager发送短信

在我的应用程序中,我不想使用默认消息发件人。 为了做到这一点,我遵循以下链接在Android中是否有可能发送短信到代码中的多个收件人?

但它不是发送sms.please帮助我如何发送短信在Android – 我已经尝试以下PendingIntent sentPI = PendingIntent.getBroadcast(this,0,new Intent(SENT),0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 

这也不起作用。
SMSAPPActivity.java

编辑:

 btnSendSMS.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String message = txtMessage.getText().toString(); String[] PhnNoArray = new String[2]; PhnNoArray[0] = "9999999999"; PhnNoArray[1] = "8888888888"; // StringTokenizer st = new StringTokenizer(phoneNo, ","); smsManager = SmsManager.getDefault(); for (int i = 0; i < PhnNoArray.length; i++) { smsManager = SmsManager.getDefault(); // this is the function that does all the magic // sms.sendTextMessage(phoneNumber, null, msg, pi, null); smsManager.sendTextMessage(PhnNoArray[i], null, message, null, null); Toast.makeText(getBaseContext(), "SMS sent : " + i, Toast.LENGTH_SHORT).show(); } } }); 

请参阅编辑并告诉我什么我做了wrong.tost显示,但通过使用此代码没有收到其他手机上的短信

Solutions Collecting From Web of "无法在Android中使用SMSManager发送短信"

我用下面的代码发送短信到多个号码和发送短信得到保存在消息

 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: ContentValues values = new ContentValues(); for (int i = 0; i < MobNumber.size() - 1; i++) { values.put("address", MobNumber.get(i).toString());// txtPhoneNo.getText().toString()); values.put("body", MessageText.getText().toString()); } getContentResolver().insert( Uri.parse("content://sms/sent"), values); 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); } 

发送消息给我使用上面的函数作为多个数字:

 if (MobNumber != null) { for (int i = 0; i < MobNumber.size(); i++) { String message = MessageText.getText().toString(); String tempMobileNumber = MobNumber.get(i).toString(); sendSMS(tempMobileNumber, message); } 

1)添加邮件而不是发件箱,因为发件箱包含假设发送或发送状态的邮件。

2)当你发送消息的同时在“ content:// sms / sent uri。

什么是停止你存储在数据库中。 还有你还试过了什么

使用下面的代码发送SMS

  smsManager.sendTextMessage(number, null,desc, null, null); 

并通过使用content:// sms / sent URI,可以将相同的文本消息插入到消息数据库中

希望这可以帮到你。

MainActivity.java

 import android.os.Bundle; import android.app.Activity; import android.telephony.SmsManager; import android.view.Menu; import android.view.inputmethod.InputMethodManager; import android.widget.*; import android.view.View.OnClickListener; import android.view.*; public class MainActivity extends Activity implements OnClickListener{ Button click; EditText txt; TextView txtvw; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); click = (Button)findViewById(R.id.button); txt = (EditText)findViewById(R.id.editText); txtvw = (TextView)findViewById(R.id.textView1); click.setOnClickListener(this); } @Override public void onClick(View v){ txt.setText(""); v = this.getCurrentFocus(); try{ SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage("8017891398",null,"Sent from Android",null,null); } catch(Exception e){ txtvw.setText("Message not sent!"); } if(v != null){ InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(),0); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

AndroidManifest.xml中添加这一行

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

在这里输入图像说明

如果您使用的是双卡设备,则必须提及发件人号码,此时您不能传递null。 否则,SmsManager将会抛出一个名为SmsManager.RESULT_ERROR_GENERIC_FAILURE的错误。

检查活跃的模拟人数的代码:

 public static int getNumberOfActiveSim(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { SubscriptionManager subscriptionManager = SubscriptionManager.from(context); List<SubscriptionInfo> subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoList(); return subscriptionInfo != null ? subscriptionInfo.size() : 0; } TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if(telephonyManager != null && telephonyManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT){ return 1; } return 0; } 

直接发送它…使用SmsManager 。 唯一的问题是,用户不知道它。

在我的情况下,这是消息正文超过160个字符,我不得不使用sendMultipartTextMessage而不是sendTextMessage