简体   繁体   English

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

[英]Unable to send sms using SMSManager in Android

In my application I do not want to use the default message sender. 在我的应用程序,我希望使用默认的邮件发件人。 For doing that I followed the following link In Android is it possible to send sms message to more than one recipient in code? 为此,我按照以下链接在Android中是否可以在代码中向多个收件人发送短信?

  • And that code worked too. 而且该代码也有效。 But the messages I am sending from this code are not saved on the phones outbox and inbox. 但是,我从此代码发送的邮件未保存在手机发件箱和收件箱中。
  • I am using sms manager like this in my code 我在我的代码中使用这样的短信管理器

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

But it is not sending sms.please help me with how can i send sms in android - i have tried following too PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent( SENT), 0); 但它不发送sms.please帮助我如何在android中发送短信 - 我已经尝试过太多PendingIntent sentPI = PendingIntent.getBroadcast(这,0,新意图(SENT),0);

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

It's also not working. 它也不起作用。
SMSAPPActivity.java SMSAPPActivity.java

EDIT : 编辑:

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

Please see the edit and tell me what i have done wrong.tost is showing up but sms is not received on other phone by using this code 请看编辑并告诉我我做错了什么.tost正在显示但是使用此代码在其他手机上没有收到短信

I used following code to send sms to multiple numbers and sent sms gets saved in messages 我使用以下代码将短信发送到多个号码,并将发送的短信保存在消息中

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

to send message to multiple numbers i used above function as : 向上面使用的多个号码发送消息的功能如下:

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

If you are using dual sim device then you must have to mention the sender number, You can't pass null at that time. 如果您使用双SIM卡设备,则必须提及发件人编号,此时您不能传递null。 Otherwise, SmsManager will throw an error called SmsManager.RESULT_ERROR_GENERIC_FAILURE . 否则,SmsManager将抛出一个名为SmsManager.RESULT_ERROR_GENERIC_FAILURE的错误。

Code to check numbers of active sims: 用于检查活动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;
        }

1) add messages in Sent instead of Outbox, as Outbox contains messages which are suppose to send or in sending state. 1)在“发送”而不是“发件箱”中添加消息,因为“发件箱”包含假定要发送或处于发送状态的消息。

2) when you send message add them at the same time in " content://sms/sent uri. 2)当你发送消息时,在“ content:// sms / sent uri”中同时添加消息。

what is stopping u to store them in database. 什么阻止你将它们存储在数据库中。 and what you tried yet. 还有你尝试过的东西

use below code to sendSMS 使用下面的代码发送SMS

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

and by using content://sms/sent URI, you can insert the same text message into Message database 通过使用content:// sms / sent URI,您可以将相同的文本消息插入Message数据库

Hope this can help you. 希望这可以帮到你。

MainActivity.java 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;
   }

}

add this line in AndroidManifest.xml AndroidManifest.xml中添加此行

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

在此输入图像描述

just send it directly... using the SmsManager . 只需直接发送...使用SmsManager Only problem is that is that the user won't know of it. 唯一的问题是用户不会知道它。

在我的情况下,事实是消息体超过160个字符,我不得不使用sendMultipartTextMessage代替sendTextMessage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM