Search code examples
androidsmssmsmanager

Android - Don't receive sms sent by sms manager


In my application I want to send an sms to a list of contacts. I used this code:

private void sendSMS(String phoneNumber, String message)
{
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, message, null, null);
        Toast.makeText(getActivity().getApplicationContext(), "SMS sent.",
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getActivity().getApplicationContext(),
                "SMS faild, please try again.",
                Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

I didn't get any stacktraces, but I never received the message. Are there any formats for the phone number? Or is an emulator not able to send text messages? Any tips on how to actually receive the message are welcome!


Solution

  • Your given code seems correct but here are two quick recommendations:

    • Make sure you have added the permission to send SMS through your app. For that you need to add <uses-permission android:name="android.permission.SEND_SMS"/> in your app's manifest
    • You cannot send SMS through emulators as they do not have access to cellular network. For that you need to use an actual phone to test your code