7

我是安卓新手。我想在点击发送按钮后发送短信

  1. 首先,我使用了短信管理器 api。
package com.example.smsproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;`enter code here`
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Page2Activity extends Activity {

  Button button;
  EditText textPhoneNo;
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      button = (Button) findViewById(R.id.button1);
      textPhoneNo = (EditText) findViewById(R.id.mobilenumber);

      button.setOnClickListener(new OnClickListener() {

          @Override

      public void onClick(View v){

      //String phoneNo = textPhoneNo.getText().toString();
      String phoneNo = "tel:xxxxxxxxxx";
      String messageText = "SMS FROM ANDROID";
      try {
          SmsManager smsManager = SmsManager.getDefault();
          smsManager.sendTextMessage(phoneNo, null, messageText, null, null);
          Toast.makeText(getApplicationContext(), "SMS Sent Successfully!",
                      Toast.LENGTH_LONG).show();
      }catch (Exception e){

          Toast.makeText(getApplicationContext(),
                  "SMS failed, please try again later ! ",
                  Toast.LENGTH_LONG).show();
          e.printStackTrace();

      }

          }

      });

  }

}
  1. 在 android_manifest.xml 上设置 send_sms 权限

我的错误为零,但没有发送短信。如果你知道答案。

请让我知道,感谢阅读。

4

5 回答 5

22

完成@Android Fanatic 的回答

如果文本太长,消息不会消失,您必须尊重最大长度,具体取决于编码。

更多信息可以在这里找到。

我更喜欢这种方法

SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);

ArrayList<PendingIntent> sendList = new ArrayList<>();
sendList.add(sentPI);

ArrayList<PendingIntent> deliverList = new ArrayList<>();
deliverList.add(deliveredPI);

sms.sendMultipartTextMessage(phoneNumber, null, parts, sendList, deliverList);
于 2015-01-28T10:32:57.680 回答
6

使用以下代码发送短信,此处错误将显示在 Toast

--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);        
}
于 2013-09-16T12:50:29.890 回答
6

如果英文文本的消息长度超过 160 条,16 位字母文本的长度超过 70 条,SMS 管理器也不会发送消息。尝试发送小的英文文本,看看是不是这样。(您可以发送多个部分消息以发送长文本)。

于 2018-05-16T12:27:47.123 回答
1
String incomming = "9876543210";
android.telephony.SmsManager sms=android.telephony.SmsManager.getDefault();
sms.sendTextMessage(incomming, null,"Here Is Sms", null, null);
于 2013-09-16T13:00:45.390 回答
0
Log.d("SMS ready to send", "----FIRST CALL----");
String number = "111111111111"; //ed1.getText().toString();
String message =  "Test SMS DATA"; //ed2.getText().toString();

Log.d("SMS ready to send", "----SECOND CALL----"+number);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number, null, message, null, null);

Log.d("SMS ready to send", "----THIRD CALL----");
于 2013-09-16T13:01:45.920 回答