My Android application is sending SMS and it's working fine. However, when the user enters a wrong number, the SMS is sent without error message. Therefore, the user doesn't know that he entered a wrong number. How can I fix that?

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);
}
有帮助吗?

解决方案

You Need to validate whether User Input is phone Number or not

For Example:Mobile No must be 10 digit

String number="2525252212"
Pattern mobileNo= Pattern.compile("\\d{10}");
Matcher matcher = mobileNo.matcher(number);
        if (matcher.matches()) {
            //go on
        } else {
            //Show Dialog 
        }

其他提示

There's a library i use for that : libphonenumber https://code.google.com/p/libphonenumber/

There are some number verification methods in there. Unless you want to implement the verifications manually.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top