Search code examples
androidgpssmsauto-responder

Automatic SMS reply from Android App


I am trying to achieve something similar to a "Find my phone" application, that is, sending an SMS to my phone and receive a reply by SMS telling where my phone is located. A requirement of this application is being open source.

I have seen that this kind of application already exists in F-Droid repository, is called FindMyPhone. However, when phone receives the SMS, its reply is just shown in stock messaging app as a reply to the contact, but not effectively sent. This is not the expected behavior, and I could see in code repository that the author tried correcting this and then gave up in 2013.

I have looked in the code and saw that the technique it uses is straigthforward:

                try {
                SmsManager smsManager = SmsManager.getDefault();
                // ********* SENDING SMS HERE *******
                smsManager.sendTextMessage(currentFromAddress, null, txt, null, null);
                // ********* SENDING SMS HERE *******
                Log.d(FindMyPhoneHelper.LOG_TAG, "Sent SMS");
                Thread.sleep(5000);
                Log.d(FindMyPhoneHelper.LOG_TAG, "Slept 5000ms");
            } catch(Exception e) {
                Log.d(FindMyPhoneHelper.LOG_TAG, "Failed SMS: " + e.getMessage());
                e.printStackTrace();
            }

(link to the code in sourceforge)

This code is executed in a class implementing a LocationListener, called inside a new Thread by a Service.

Any hint about the reason of this behavior? Why message is just shown but not sent? I have looked in another similar application: https://f-droid.org/repository/browse/?fdid=com.teamdc.stephendiniz.autoaway

This is working ok and answers to SMSs as expected. Its main difference is that this app runs in a service continuosly listening for new messages and registers the receiver on the fly, rather than declaring it in the manifest. For me this won't be a big deal, but I'd prefer not having a service always running.

Thanks in advance for any help on this.


Solution

  • Found out what the problem was, it has nothing to do with messaging API.

    In the BroadcastReceiver reacting to the SMS arrival, phone number of the sender was passed to the Service actually sending the reply SMS by Intent data like this

    locationIntent.setData(Uri.parse("?destinationAddress=" + from));
    

    As the "from" address was URI encoded, the + (plus) character in the international prefix was lost. Messaging app could live with that, so it was able to show the message, but this wouldn't be sent.

    Using Intent Extras instead of Data did the trick, as this way phone number won't pass through any encoding.

    All the best