Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Android中发送短信大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码将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中发送短信所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。