在Android中发送短信 [英] Sending SMS in Android

查看:140
本文介绍了在Android中发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用下面的code发送短信到Android手机。但是,如果我派短信以任何数量的它没有被发送。可能是什么问题?

 包SMSApp.com;

进口android.app.Activity;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.os.Bundle;
进口android.telephony.SmsManager;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;

公共类短信延伸活动{
    / **第一次创建活动时调用。 * /

    按钮btnSendSMS;
    的EditText txtPhoneNo;
    的EditText txtMessage;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        btnSendSMS =(按钮)findViewById(R.id.btnSendSMS);
        txtPhoneNo =(EditText上)findViewById(R.id.txtPhoneNo);
        txtMessage =(EditText上)findViewById(R.id.txtMessage);

        btnSendSMS.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                字符串PHONENO = txtPhoneNo.getText()的toString()。
                字符串消息= txtMessage.getText()的toString()。
                如果(phoneNo.length()大于0&安培;&安培; message.length()大于0)
                    sendSMS(PHONENO,消息);
                其他
                    Toast.makeText(getBaseContext(),
                        请同时输入电话号码和信息。
                        Toast.LENGTH_SHORT).show();
            }

            / *私人无效sendSMS(PHONENO字符串,字符串消息){
                // TODO自动生成方法存根

                PendingIntent圆周率= PendingIntent.getActivity(SMS.this,0,新意图(SMS.this,SMS.class),0);
                    SmsManager的短信= SmsManager.getDefault();
                    sms.sendTextMessage(PHONENO,空,消息,PI,NULL);
            } * /

            私人无效sendSMS(字符串phoneNumber的,字符串消息)
            {
                字符串SENT =SMS_SENT;
                字符串DELIVERED =SMS_DELIVERED;

                PendingIntent sentPI = PendingIntent.getBroadcast(SMS.this,0,
                    新的意图(SENT),0);

                PendingIntent deliveredPI = PendingIntent.getBroadcast(SMS.this,0,
                    新的意图(交付),0);

                // ---当短信已发送---
                registerReceiver(新BroadcastReceiver的(){
                    @覆盖
                    公共无效的onReceive(背景为arg0,意图ARG1){
                        开关(的getResult code())
                        {
                            案例Activity.RESULT_OK:
                                Toast.makeText(getBaseContext(),短信发送
                                        Toast.LENGTH_SHORT).show();
                                打破;
                            案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                                Toast.makeText(getBaseContext(),一般故障,
                                        Toast.LENGTH_SHORT).show();
                                打破;
                            案例SmsManager.RESULT_ERROR_NO_SERVICE:
                                Toast.makeText(getBaseContext(),无服务,
                                        Toast.LENGTH_SHORT).show();
                                打破;
                            案例SmsManager.RESULT_ERROR_NULL_PDU:
                                Toast.makeText(getBaseContext(),空的PDU,
                                        Toast.LENGTH_SHORT).show();
                                打破;
                            案例SmsManager.RESULT_ERROR_RADIO_OFF:
                                Toast.makeText(getBaseContext(),无线电关
                                        Toast.LENGTH_SHORT).show();
                                打破;
                        }
                    }
                },新的IntentFilter(发送));

                // ---当SMS已交付---
                registerReceiver(新BroadcastReceiver的(){
                    @覆盖
                    公共无效的onReceive(背景为arg0,意图ARG1){
                        开关(的getResult code())
                        {
                            案例Activity.RESULT_OK:
                                Toast.makeText(getBaseContext(),SMS递送,
                                        Toast.LENGTH_SHORT).show();
                                打破;
                            案例Activity.RESULT_CANCELED:
                                Toast.makeText(getBaseContext(),短信未交付,
                                        Toast.LENGTH_SHORT).show();
                                打破;
                        }
                    }
                },新的IntentFilter(交付));

                SmsManager的短信= SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);
            }
        });
    }
}
 

解决方案

我真的不明白你为什么会嵌套sendSMS方法呢 - 在code对我的作品。尝试,如果你得到了

检查

 <使用-权限的Andr​​oid:名称=android.permission.SEND_SMS>< /使用-许可>
 

权限集中在你的文件,并尝试这个code来代替:

 包SMSApp.com;
进口android.app.Activity;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.os.Bundle;
进口android.telephony.SmsManager;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;

公共类短信延伸活动{
    / **第一次创建活动时调用。 * /

    按钮btnSendSMS;
    的EditText txtPhoneNo;
    的EditText txtMessage;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        btnSendSMS =(按钮)findViewById(R.id.btnSendSMS);
        txtPhoneNo =(EditText上)findViewById(R.id.txtPhoneNo);
        txtMessage =(EditText上)findViewById(R.id.txtMessage);

        btnSendSMS.setOnClickListener(新View.OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                字符串PHONENO = txtPhoneNo.getText()的toString()。
                字符串消息= txtMessage.getText()的toString()。
                如果(phoneNo.length()大于0&安培;&安培; message.length()大于0)
                    sendSMS(PHONENO,消息);
                其他
                    Toast.makeText(getBaseContext(),
                                   请同时输入电话号码和信息。
                                   Toast.LENGTH_SHORT).show();
            }

        });
    }

    私人无效sendSMS(字符串phoneNumber的,字符串消息)
    {
        字符串SENT =SMS_SENT;
        字符串DELIVERED =SMS_DELIVERED;

        PendingIntent sentPI = PendingIntent.getBroadcast(SMS.this,0,
            新的意图(SENT),0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(SMS.this,0,
            新的意图(交付),0);

        // ---当短信已发送---
        registerReceiver(新BroadcastReceiver的(){
            @覆盖
            公共无效的onReceive(背景为arg0,意图ARG1){
                开关(的getResult code())
                {
                    案例Activity.RESULT_OK:
                        Toast.makeText(SMS.this,短信发送
                                Toast.LENGTH_SHORT).show();
                        打破;
                    案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(SMS.this,一般故障,
                                Toast.LENGTH_SHORT).show();
                        打破;
                    案例SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(SMS.this,无服务,
                                Toast.LENGTH_SHORT).show();
                        打破;
                    案例SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(SMS.this,空PDU
                                Toast.LENGTH_SHORT).show();
                        打破;
                    案例SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(),无线电关
                                Toast.LENGTH_SHORT).show();
                        打破;
                }
            }
        },新的IntentFilter(发送));

        // ---当SMS已交付。 ---
        registerReceiver(新BroadcastReceiver的(){
            @覆盖
            公共无效的onReceive(背景为arg0,意图ARG1){
                开关(的getResult code())
                {
                    案例Activity.RESULT_OK:
                        Toast.makeText(SMS.this,短信发送
                                Toast.LENGTH_SHORT).show();
                        打破;
                    案例Activity.RESULT_CANCELED:
                        Toast.makeText(SMS.this,短信未交付,
                                Toast.LENGTH_SHORT).show();
                        打破;
                }
            }
        },新的IntentFilter(交付));

        SmsManager的短信= SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);
    }
}
 

I am using the below code to send SMS to an Android mobile. But if I send the SMS to any number it's not being sent. What could be the issue?

package SMSApp.com;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMS extends Activity {
    /** Called when the activity is first created. */

    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;

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

        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);

        btnSendSMS.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                String phoneNo = txtPhoneNo.getText().toString();
                String message = txtMessage.getText().toString();
                if (phoneNo.length()>0 && message.length()>0)
                    sendSMS(phoneNo, message);
                else
                    Toast.makeText(getBaseContext(),
                        "Please enter both phone number and message.",
                        Toast.LENGTH_SHORT).show();
            }

            /*private void sendSMS(String phoneNo, String message) {
                // TODO Auto-generated method stub

                PendingIntent pi = PendingIntent.getActivity(SMS.this, 0, new Intent(SMS.this, SMS.class), 0);
                    SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage(phoneNo, null, message, pi, null);
            }*/

            private void sendSMS(String phoneNumber, String message)
            {
                String SENT = "SMS_SENT";
                String DELIVERED = "SMS_DELIVERED";

                PendingIntent sentPI = PendingIntent.getBroadcast(SMS.this, 0,
                    new Intent(SENT), 0);

                PendingIntent deliveredPI = PendingIntent.getBroadcast(SMS.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);
            }
        });
    }
}

解决方案

I really don't get why would you nest the sendSMS method anyway - the code works for me. Try checking if you got the

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

permission set in your file and try this code instead:

package SMSApp.com;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMS extends Activity {
    /** Called when the activity is first created. */

    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;

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

        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);

        btnSendSMS.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                String phoneNo = txtPhoneNo.getText().toString();
                String message = txtMessage.getText().toString();
                if (phoneNo.length()>0 && message.length()>0)
                    sendSMS(phoneNo, message);
                else
                    Toast.makeText(getBaseContext(),
                                   "Please enter both phone number and message.",
                                   Toast.LENGTH_SHORT).show();
            }

        });
    }

    private void sendSMS(String phoneNumber, String message)
    {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(SMS.this, 0,
            new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(SMS.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(SMS.this, "SMS sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(SMS.this, "Generic failure",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(SMS.this, "No service",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(SMS.this, "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(SMS.this, "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(SMS.this, "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(DELIVERED));

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

这篇关于在Android中发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆