1

I am very sorry if this is a duplicate post, but believe me I did a lot of searching. Way back in android 2.2 I had an application with a static broadcast receiver that would get called each time a new text message arrived, regardless of applications state.

Now, I am trying to have same behavior, but on android 5 (I believe this to be post 4.4 thing). As soon as my app is closed from recent apps, static receiver stops working.

Is this how Android works now? I have found one answer on stackoverflow saying that this is so, but I saw no documentation.

Perhaps something is missing here:

        <receiver
        android:name="com.dimitar.android.test.comm.ControlMessagesReceiver"   
        android:exported="true"      
        android:enabled="true"
        android:permission="android.permission.BROADCAST_SMS" >
        <intent-filter>                
            <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter>
    </receiver>

If so, then my only idea is to listen to boot event and start a service to handle what I need.

Dimitar Vukman
  • 421
  • 6
  • 15

2 Answers2

3

There are some changes for SMS. Check this example to correctly use BroadcastReceiver for SMS.

Firstly, you’ll need the RECEIVE_SMS permission, so put this in your manifest:

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

Add receiver configuration to AndroidManifest.xml:

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

Finally, implement receiver class:

public class SmsReceiver extends BroadcastReceiver {
    private String TAG = SmsReceiver.class.getSimpleName();

    public SmsReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // Get the data (SMS data) bound to intent
        Bundle bundle = intent.getExtras();

        SmsMessage[] msgs = null;
        if (bundle != null) {
            // Retrieve the SMS Messages received
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];

            // your code here ...
        }
    }
}

You can find more details in this blog post "Android Send and Receive SMS".

comrade
  • 4,590
  • 5
  • 33
  • 48
  • comrade, thanks for the answer. This works fine, but as soon as I swipe the app from recent apps list, it stops working. As if android un-registers static listener. I'd like to get event even though my app is closed. – Dimitar Vukman Jun 24 '16 at 18:08
  • Aha, in this case it's unfortunately impossible as I see. Check up this SO question [Can non-default SMS app ALWAYS receive broadcast when SMS received, even when force closed?](http://stackoverflow.com/questions/34012158/can-non-default-sms-app-always-receive-broadcast-when-sms-received-even-when-fo) – comrade Jun 24 '16 at 18:55
  • Thanks comrade. I m buying a beer. I dont know why they made such a change. Limits possibilities... – Dimitar Vukman Jun 24 '16 at 19:36
0

I should have probably said that I am testing on Xiaomi R. Note 3 device with android 5.

Looks like Xiaomi has a Security application that controls pretty much everything. See another question and answer here

Community
  • 1
  • 1
Dimitar Vukman
  • 421
  • 6
  • 15