在Android应用程序中发送短信

在Android应用程序中发送短信,android,Android,我提出了一个发送申请。在没有实际设备的情况下,如何检查邮件发送 sendSms.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //Toast.makeText(getApplicationContext(),template+"and"+phoneNumber,Toast.LENGTH_LONG).show(); if (phoneNumb

我提出了一个发送申请。在没有实际设备的情况下,如何检查邮件发送

sendSms.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View v)
    {
        //Toast.makeText(getApplicationContext(),template+"and"+phoneNumber,Toast.LENGTH_LONG).show();
        if (phoneNumber.length()>0 && template.length()>0)
        {
            sendSMS(phoneNumber, template);
        }
        else
            Toast.makeText(getApplicationContext(), "please select name & template",Toast.LENGTH_SHORT).show();
    }

    private void sendSMS(String phoneNumber, String template)
    {
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,new Intent(getApplicationContext(),Test2Activity.class), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, template, pi, null);
    }
});
  • 打开两个模拟器
  • 在一个模拟器中运行您的程序
  • 在“sms.sendTextMessage(phoneNumber,null,template,pi,null);”行中,用仿真器号替换phoneNumber。例如,5554/5556等
  • 成功运行代码后,另一个仿真器将收到SMS
  • 打开两个模拟器
  • 在一个模拟器中运行您的程序
  • 在“sms.sendTextMessage(phoneNumber,null,template,pi,null);”行中,用仿真器号替换phoneNumber。例如,5554/5556等
  • 成功运行代码后,另一个仿真器将收到SMS

  • 您可以这样检查:-

    registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context context, Intent intent) {
                     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));
    
    
    registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                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));
    

    Umesh的答案绝对正确,但如果您想实际检查,则必须在活动中添加此代码。

    您可以这样检查:-

    registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context context, Intent intent) {
                     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));
    
    
    registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                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));
    

    Umesh的回答绝对正确,但如果您想实际检查,则必须在活动中添加此代码。

    欢迎。如果这解决了你的问题,考虑接受答案。单击左侧的勾号。将鼠标悬停,它将出现。大家好,此代码仅适用于移动设备中没有国家代码的号码,但如果我尝试使用带有国家代码的号码,则其不起作用,而对于国际邮件,我的意思是如何发送国际邮件?欢迎。如果这解决了你的问题,考虑接受答案。单击左侧的勾号。将鼠标悬停,它会出现。大家好,此代码仅适用于移动设备中没有国家代码的号码,但如果我尝试使用带有国家代码的号码,则其不起作用,而对于国际邮件,我的意思是如何发送国际邮件?