2

How can I receive SMS messages when app is closed? Following code works fine, but when app is closed, e.g. after reboot, It doesn't work. (Actually, it works just first few minutes after app is closed, Strange...)

My AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
    <activity ....>
    ....
    </activity>

    <receiver android:name=".Receiver" android:exported="true" android:enabled="true">
        <intent-filter android:priority="1">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

My Receiver.java

public class Receiver extends BroadcastReceiver
{
    @Override public void onReceive(Context context, Intent intent)
    {
        if(cond)
        {
            abortBroadcast();
            Toast.makeText(context, "Registration Completed", Toast.LENGTH_LONG).show();
        }
    }
}
Noah
  • 21
  • 1
  • 2

1 Answers1

1

If you want to continue receiving SMS after your app is closed, you should implement all the stuff relied to SMS receiving in a Service that won't be closing with your application. Refer this sample to do this:

Having a Service receive SMS messages

Community
  • 1
  • 1
floyd
  • 692
  • 8
  • 23
  • Thank you for answering this thread. I created service class based on that answer, but it seems that it doesn't work. Can you tell me that am I setting manifest correctly, please? – Noah May 17 '15 at 21:06
  • I'm still looking for a solution. Can you please help, if you can? – Noah May 18 '15 at 20:49
  • Did you try to debug your application? Your app is able to successfully create a service? Problem of your manifest may be is that some other app has a higher priority than yours, so you can try to set it bigger. – floyd May 19 '15 at 21:18