8

I am developing an anti-theft application which is based on starting the service using a broadcast receiver for an incoming SMS.

But if the phone is already lost, the broadcast receiver won't work when the application is remotely installed from Google Play as the application has to be started at least once in order to receive broadcast for version 3.0+.

So, is there a way to start the application right after installation using some "helper application" or make broadcast receiver work for remote installation?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
test
  • 546
  • 1
  • 6
  • 18
  • http://stackoverflow.com/questions/11872320/auto-launching-android-app-after-install – KOTIOS Jul 25 '13 at 07:12
  • there is an app on google play [Android Lost](https://play.google.com/store/apps/details?id=com.androidlost&hl=en) which invoke the registration service for google push messages via an incoming sms without launching the app even once for version 3.0+ – test Jul 25 '13 at 08:41

1 Answers1

0

Your application will need to have the uses-permission for android.permission.RECEIVE_SMS in your manifest.

Once you have that you can register a broadcast receiver for android.provider.Telephony.SMS_RECEIVED.

Then you'll want to create your receiver.

<receiver android:name=".SMSBroadcastReceiver"> 
    <intent-filter> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver>

Your receiver should extend the BroadcastReceiver and in the onReceive() method when you receive an intent for android.provider.Telephony.SMS_RECEIVED_ACTION you want to retrieve the message and determine if it is one that you want to pay attention to.

Your code might look something like this.

public class SMSBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "SMSBroadcastReceiver";
    private static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SMS_RECEIVED)) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                final SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }
                if (messages.length > -1) {
                    //You have messages, do something with them here to determine if you want to look at them and other actions.
                }
            }
        }
    }
}
David
  • 7,005
  • 2
  • 22
  • 29
  • Buddy you didn't understand the problem. Please read the question again. – test Aug 02 '13 at 09:29
  • You are attempting to load your solution on the native application, but given the scenario provided the user would not likely have access to their application to register the phone. Instead I would do a remote install, register the receiver (done automatically) and then have the user input their number on a website or other platform. Once done, wait a few minutes send text to number they provided, [launch service](http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)) and then go on your merry way. – David Aug 02 '13 at 18:45
  • sorry didn't get you. Are you aware of the fact that broadcast receiver won't work if the application is not launched at least once (for version 3.0 on wards). – test Aug 02 '13 at 18:57
  • I was not aware of this, good to know. Taking a look [here](http://developer.android.com/about/versions/android-3.1.html#launchcontrols) it looks like you may be able to use [FLAG_INCLUDE_STOPPED_PACKAGES](http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES) to overcome this restriction. From my understanding of reading the changes the difference is that the application is considered to be in a stopped state which causes the Broadcast Receivers to not be called, so setting the flag would cause it to be available and alleviate your problem. – David Aug 05 '13 at 13:19