Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 发送短信时出错_Java_Android_Sms - Fatal编程技术网

Java 发送短信时出错

Java 发送短信时出错,java,android,sms,Java,Android,Sms,我的Android应用程序正在发送短信,而且运行良好。但是,当用户输入错误的号码时,发送的SMS没有错误消息。因此,用户不知道他输入了错误的号码。我怎样才能解决这个问题 Activity.java StringTokenizer st=new StringTokenizer(phoneNo,","); while (st.hasMoreElements()) { String tempMobileNumber = (String)st.nextElement(); if(temp

我的Android应用程序正在发送短信,而且运行良好。但是,当用户输入错误的号码时,发送的SMS没有错误消息。因此,用户不知道他输入了错误的号码。我怎样才能解决这个问题

Activity.java

StringTokenizer st=new StringTokenizer(phoneNo,",");
while (st.hasMoreElements())
{
    String tempMobileNumber = (String)st.nextElement();
    if(tempMobileNumber.length()>0 && sms.trim().length()>0)
    {
        sendSMS(tempMobileNumber, sms);
    }
    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(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);
}

您需要验证用户输入是否为电话号码

例如:手机号码必须是10位数字


我使用了一个库:libphonenumber


这里有一些数字验证方法。除非您想手动执行验证。

加上一个用于验证建议以及模式匹配器。
String number="2525252212"
Pattern mobileNo= Pattern.compile("\\d{10}");
Matcher matcher = mobileNo.matcher(number);
        if (matcher.matches()) {
            //go on
        } else {
            //Show Dialog 
        }