-1

Can anyone please tell me if it is possible to "wake up" my Android service as soon as there's an incoming SMS event? What I meant to say is that, whenever an SMS is received, there will be a system-wide broadcast of this event. Can I intercept this broadcast to start my own background service? I know, it's not possible to run my service permanently in the background (not to mention it's a bad design practice).

Any help would be highly appreciated!

EDIT: Adding more detail to my original question. My broadcast receiver will be wrapped inside a service class. But there is a chance the Android will kill my background service in the event of memory crunch. In that case, even if there is an incoming SMS, my service won't be fired. How can I deal with this situation? This was the main intent of the question. I know it's not possible to run my service permanently in the background (or is it possible? perhaps if I have root access?)

Vinit Shandilya
  • 1,643
  • 5
  • 24
  • 44
  • yes you can do it. you can register for the sms received broadcast receiver . and you will be notified when device will get a new sms. – Sagar Nayak Jul 04 '16 at 10:32
  • 1
    Why are you doubting if it will work? If you try it you will see it works fine – Tim Jul 04 '16 at 10:44
  • Yes you can, because inside `onReceive` of the`BroadcastReceiver`, there is a `context` as a parameter. So you have the possibility to start your Application or to start a new service etc. – VasFou Jul 04 '16 at 10:47

1 Answers1

0

Create Broadcast Receiver

public class BrodcastReceiverClass extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) {


        final Bundle bundle = intent.getExtras();
        try {
            if (bundle != null) {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();
                    try {
                        if (senderNum.equals("MD-DOCOMO")){  //SMS Provider Name
                            OTPActivity Sms = new OTPActivity();
                            Sms.recivedSms(message);
                        }
                    } catch (Exception e) {
                    }

                }
            }

        } catch (Exception e) {

        }
    }
} 

Register BrodcastReceiver in your manifest

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

call in you activity for e.g your activity name is OTPActivity than write thi code in your OTPActivity.

public void recivedSms(String message) {
        try {
            Log.d("message is receive", message);



        } catch (Exception e) {
            Log.e("message not receive", e.getMessage() + "");
        }
    }

Add permission in manifest read sms

    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
Jatin Devani
  • 190
  • 3
  • 15
  • *call in you activity for e.g your activity name is OTPActivity than write thi code in your OTPActivity.* that is absolutely not necessary. Just handle it in the broadcastreceiver – Tim Jul 04 '16 at 10:55
  • `OTPActivity Sms = new OTPActivity();` **never** manually instantiate activities, especially not from code that runs when your app is not open – Tim Jul 04 '16 at 10:56
  • @TimCastelijns we write this code because OTPActivity than write thi code in your OTPActivity. if we required automatic OTP (sms) verification than brodcast receiver send to msg in OTPActivity activity – Jatin Devani Jul 04 '16 at 11:16
  • Yeah but you have no idea what OP wants to do with the SMS so you cannot just assume that – Tim Jul 04 '16 at 11:21
  • Thanks a lot! My broadcast receiver will be wrapped inside a service class. But there is a chance the Android will kill my background service in the event of memory crunch. In that case, even if there is an incoming SMS, my service won't be fired. How can I deal with this situation? – Vinit Shandilya Jul 04 '16 at 11:24
  • This requires the `RECEIVE_SMS` permission, not `READ_SMS`. And your message handling is a little off. Each time the Receiver fires, it gets only one message. You need to concatenate the parts created in the `for` loop to get the complete message, if it's multipart. – Mike M. Jul 04 '16 at 19:14