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

14

我的应用程序中不想使用默认的消息发送器。为了达到这个目的,我按照以下链接中的说明进行操作:在Android中是否可以通过代码向多个收件人发送短信?

  • 这段代码也起作用了。但是,我从这个代码发送的短信没有保存在手机的发件箱和收件箱中。
  • 我在我的代码中像这样使用短信管理器

    SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, null, null);

但它没有发送短信。请帮助我了解如何在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();
                }
}
        });
请查看编辑内容并告诉我哪里做错了。使用此代码,tost已经出现,但其他手机没有收到短信。

你所说的“默认消息发送器”是什么意思?请明确说明。 - JoxTraex
@JoxTraex:默认情况下,消息发送者是指我不想打开系统的消息应用程序来发送消息,我只想从我的应用程序中发送消息... - anon
我正在这里做类似的事情!!! https://dev59.com/j-o6XIcBkEYKwwoYORwV - Etienne Lawlor
5个回答

4
如果您使用双卡设备,则必须提及发送者号码,此时不能传递null。否则,SmsManager将抛出一个名为SmsManager.RESULT_ERROR_GENERIC_FAILURE的错误。
检查活动sim卡数量的代码:
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;
        }

3

1) 将邮件添加到已发送中而不是发件箱中,因为发件箱包含正在发送或即将发送的邮件。

2) 当您发送邮件时,请同时将它们添加到“content://sms/sent” uri 中。

什么阻止您将其存储在数据库中?您已经尝试了什么。

使用以下代码来发送短信:

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

通过使用content://sms/sent URI,您可以将相同的短信插入到信息数据库中。


谢谢回复。请看我的编辑,我已经添加了代码。它会在短信发送后显示提示信息。 - anon
我编辑了答案,请仔细阅读。上述行的“content://sms / sent”是内容URI,指向已发送信息的数据库表,将您的消息插入此URI中,因此消息将被发送并插入到消息数据库中,这也会反映在消息应用程序中... - AAnkit
但我必须发送消息到多个号码 - anon
嗨,@Ankita,我尝试使用循环发送消息,但它抛出错误,请查看我的编辑代码。 - anon
  1. 首先是Ankit A,不是Ankita。
  2. 这个错误出现在第一条信息中。因为错误显示目标地址不正确,请检查它。或者通过Log.d()在循环内部打印您的目标地址/电话号码,不要无谓地生成toast。
  3. 您是否有发送短信权限?
- AAnkit

2
希望这能帮到你。 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" />

enter image description here


1

直接使用SmsManager发送即可。唯一的问题是用户不会知道。


0
在我的情况下,问题是消息正文超过了160个字符,因此我不得不使用sendMultipartTextMessage代替sendTextMessage

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接