5

Hey, I have been making an Android app which needs to send a text message. Here is the current code I have:

public class getMessage extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    client Phone = new client();


    String[] msg = Phone.getMsg(user[0],user[1]);
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, getMessage.class), 0);
    SmsManager man = SmsManager.getDefault();
    Log.e("GOT MESSAGE", msg[0]+ " : " +msg[1]);
    man.sendTextMessage(msg[0], null, msg[1], pi, null);
    Log.e("Message", "Sent the message?");
}

Now, for some reason, the text message will not send using this code, and I'm not sure why. I was hoping that someone here could help me out in finding why this message won't send.

No error is raised, nothing appears in the log (except for the log messages that I make myself in the code). Also, the manifest does have the correct tags.

Suggestions?

D4N14L
  • 440
  • 1
  • 6
  • 10
  • FWIW, here is a sample project that works, but it is from an activity, not a service: https://github.com/commonsguy/cw-advandroid/tree/master/SMS/Sender – CommonsWare Jan 05 '11 at 23:22
  • What does Phone.getMsg(user[0], user[1]) return? Have you tried this on a device or emulator? Also, your class names should always start with an upper case letter (GetMessage not getMessage). – Eric Levine Jan 05 '11 at 23:22
  • Phone.getMessage gets the message that it received... and getMessage is a method, not a class, though my client class should be capitalized... – D4N14L Jan 06 '11 at 01:26
  • Whoops... getMsg gets the recipient of the message being sent(msg[0]), as well as the message (msg[1]) – D4N14L Jan 06 '11 at 01:30
  • did you get your app working i got my app working but i am looing for someone to help me. i need to change it to send out one at a time and wait for the return code before sending the next one – SJS Jan 21 '11 at 20:52

3 Answers3

3

You should check if you have permission for sending SMS.

Ante
  • 8,567
  • 17
  • 58
  • 70
2

Well, now, this is embarrassing. Apparently, the way I was doing it WAS correct. Just the way that I was checking to see if it was sent was apparently incorrect.

When sending through code like this, it does not appear as a sent message on the phone, but will be received by the other person. I just had to test this by running two emulators.

Thanks for the help anyways, guys!

D4N14L
  • 440
  • 1
  • 6
  • 10
1

does this link help? It basically goes over a sample activity that sends and listens for SMS messages.

ekawas
  • 6,594
  • 3
  • 27
  • 39
  • Unfortunately, no. I have seen that link before, and it basically goes over what I had already done. Thanks for trying, though. – D4N14L Jan 06 '11 at 04:40
  • I know that you said that you have the correct permissions, but I think that I read somewhere (cant find it right now) that you also need to add READ_PHONE_STATE for sending SMS messages. Again, this is just something that I think that I read a while ago... – ekawas Jan 06 '11 at 17:35
  • one other thing, how are you testing this? I noticed that when I test on an actual device, the log isnt 100% accurate. Try testing with the emulator and looking for messages in Logcat. – ekawas Jan 10 '11 at 14:41