在Android中发送短信

在Android中发送短信,第1张

概述我使用以下代码将SMS发送到 Android手机.但是,如果我将 SMS发送到任何号码,它就不会被发送.可能是什么问题? package SMSApp.com;import android.app.Activity;import android.app.PendingIntent;import android.content.BroadcastReceiver;import androi 我使用以下代码将SMS发送到 Android手机.但是,如果我将 SMS发送到任何号码,它就不会被发送.可能是什么问题?
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,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,new Intent(SENT),0);                PendingIntent deliveredPI = PendingIntent.getbroadcast(SMS.this,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,"SMS delivered",Toast.LENGTH_SHORT).show();                                break;                            case Activity.RESulT_CANCELED:                                Toast.makeText(getBaseContext(),"SMS not delivered",new IntentFilter(DEliVERED));                SmsManager sms = SmsManager.getDefault();                sms.sendTextMessage(phoneNumber,sentPI,deliveredPI);            }        });    }}
解决方法 我真的不明白你为什么要嵌套sendSMS方法 – 代码对我有用.试试检查你是否得到了
<uses-permission androID:name="androID.permission.SEND_SMS"></uses-permission>

在您的文件中设置权限并尝试使用此代码:

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,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);        PendingIntent deliveredPI = PendingIntent.getbroadcast(SMS.this,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,Toast.LENGTH_SHORT).show();                        break;                    case SmsManager.RESulT_ERROR_GENERIC_FAILURE:                        Toast.makeText(SMS.this,Toast.LENGTH_SHORT).show();                        break;                    case SmsManager.RESulT_ERROR_NO_SERVICE:                        Toast.makeText(SMS.this,Toast.LENGTH_SHORT).show();                        break;                    case SmsManager.RESulT_ERROR_NulL_PDU:                        Toast.makeText(SMS.this,Toast.LENGTH_SHORT).show();                        break;                    case SmsManager.RESulT_ERROR_RAdio_OFF:                        Toast.makeText(getBaseContext(),Toast.LENGTH_SHORT).show();                        break;                }            }        },new IntentFilter(SENT));        //--- When the SMS has been delivered. ---        registerReceiver(new broadcastReceiver(){            @OverrIDe            public voID onReceive(Context arg0,Toast.LENGTH_SHORT).show();                        break;                    case Activity.RESulT_CANCELED:                        Toast.makeText(SMS.this,new IntentFilter(DEliVERED));        SmsManager sms = SmsManager.getDefault();        sms.sendTextMessage(phoneNumber,deliveredPI);    }}
总结

以上是内存溢出为你收集整理的在Android中发送短信全部内容,希望文章能够帮你解决在Android中发送短信所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1137017.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存