0

So last week my phone got stolen and the wifi and mobile data were turned off so I had no access to it. GPS was turned off as well but that's of no use if the phone doesn't have an internet connection. Even though the phone was protected by a fingerprint and my data would be safe, I still would've loved that phone back. Thinking about this I came up with an idea.

What if I create an Android app that would run silently in the background on my (new)phone and would do absolutely nothing. All it would do is listen for a certain string of characters in all the SMS the phone receives. Now say my new phone gets stolen too (talk about tough luck). I immediately call at my number from someone's phone. If the phone is still turned on and has cellular connectivity, I would send an SMS with THAT string of characters to my number. As soon as the phone receives the SMS, the app picks up on it and does the following:

  1. Turns on GPS.
  2. Gets a lock on its position.
  3. Starts sending its own coordinates to the number it received the SMS from at a rate of once every minute via SMS.

This way I get to know where my phone is and the thief won't know whats happening because everything is being done silently and the phone is locked as well. The main thing I want to cover with this app is the fact that this doesn't rely on an internet connection. It just trusts the fact that only I know the certain string of characters and that the app exists and is running 24/7. I know this isn't suitable for public use but for personal use, its ok I guess.

What I want to ask for is advice on what approach should I take? Any suggestions about things I should add? Have you ever tried something like this and if so, then can you share your experience with me?

Thanks in advance!

Owais
  • 21
  • 4

1 Answers1

0

I have done in my project. Hope it will help you. Ask if it not clear to you. First you need to create SMSReceiver class for receiving sms into application which extend broadcast receiver & SMSListener interface for listening the SMS data and pass it to your desired activity.

Here is the code:

public class SmsReceiver extends BroadcastReceiver {

private static SmsListener mListener;

@Override
public void onReceive(Context context, Intent intent) {
    Bundle data  = intent.getExtras();

    Object[] pdus = (Object[]) data.get("pdus");//pdus is the protocol of SMS data.

    for(int i=0;i<pdus.length;i++){
        SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);

        String sender = smsMessage.getDisplayOriginatingAddress();
        //You must check here if the sender is your provider and not another one with same text.

        String messageBody = smsMessage.getMessageBody(); //SMS text data

        mListener.messageReceived(messageBody);//Pass on the text to our listener.
    }

}

public static void bindListener(SmsListener listener) {
    mListener = listener;
}

public static void unbindListener() {
    mListener = null;
}


public interface SmsListener {
void messageReceived(String messageText);
}
}

Then you need to declare your receiver in manifest file. Like below.

<receiver android:name=".SmsReceiver" android:permission="android.permission.BROADCAST_SMS">
      <intent-filter android:priority="999"> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
      </intent-filter>
</receiver>

Then finally in your activity add this code for getting SMS data.

 public Pattern p = Pattern.compile("(|^)\\d{4}");  //It will detect 4 number OTP value.
 SmsReceiver.bindListener(new SmsListener() {
        @Override
        public void messageReceived(String messageText) {
            Log.d("MESSAGE TEXT",messageText);
            if(messageText != null)
            {
                Matcher m = p.matcher(messageText);
                if(m.find()) {
                    //Do your code here after your sms received.
                }
                else
                {
                    //Do here if you want to trigger anyof sms received.
                }
            }
        }
    });

BroadcastReceiver always listening for the incoming SMS. If it fails to trigger at app close or screen close state or always running issue look at this solution will help you.

Keep broadcast receiver running after application is closed

EDITED

Have a look at some workground scenario of never end service. All you need is monitoring the service & restart if killed. Also you can store the status of service in shared preference as well. Here some of the options & workcode for never end service. You can find more on search.

Android: keep Service running when app is killed

Creating a never ending background service in Android

Hope it will helps.

Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24
  • Thank you so much for the contribution. I'm not a skilled developer in any way. I want the app to run as a system service in the background all the time with even the task manager not being able to close it. Also, I want it to start when the device boots up. How do I do that? – Owais Jul 17 '18 at 10:11
  • See my edited section for never end service workground. You have more way to do it. – Mohamed Mohaideen AH Jul 18 '18 at 09:00