7

What I want: I want to be the first to receive the Sms Broadcast and I want to cancel the broadcast if SMS is of my interest only, so that The broadcast doesn't reach any other app/receiver (Default messaging app etc.). What I know is:

  • SmsDisptacher.java uses orderedBroadcasts that can be can canceled/aborted.

What I don't know is:

  • If orderedBrodcasts can be canceled for other apps/receivers i.e other than yourself.

what I have tried for being the first to receive the Broadcast:

  • intent-filter android:priority="1000"

What i have tried for canceling broadcast already:

  • AbortBroadcast();
  • broadcastReceiver.setResultCode(RESULT_CANCELED)
  • clearAbortBroadcast()
hassanadnan
  • 435
  • 3
  • 8
  • 17

3 Answers3

11

It is certainly possible. Register your receiver in your manifest like this:

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

In your receiver's onReceive method write:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(SMS_RECEIVED)) {
        //do your stuff
        abortBroadcast();
    }
}

Remember to set the permission to receive sms messages in the manifest like this:

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

Without the correct permission set it will fail silently with the following logcat message:

07-13 12:54:13.208: WARN/ActivityManager(201): Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } to com.mypackage requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001)

Finally, it is possible that another application has registered a receiver with an even higher priority than yours, and that it therefore receives the broadcast before you cancel it. So if all else fails, try experimenting with higher priority values.

Marmoy
  • 8,009
  • 7
  • 46
  • 74
  • what if i dont want to touch the manifestfile.... is that the method of registerReceiver(smsSentReceiver, new IntentFilter(TAG_SENT));" always working? @Videre – gumuruh Aug 06 '14 at 17:49
  • @gumuruh I would expect that registering the receiver during runtime with Context.registerReceiver only ensures reception of broadcast as long as the application is running. This means that a 'sleeping' application will not be woken up by the system when a message is received, unless it is registered in the manifest. Someone correct me if I'm wrong. – Marmoy Aug 08 '14 at 08:47
1

it's possible to do that, i did a tutorial check it please here and here just abort the broadcast on the top of the hierarchy

Joao Ismael
  • 177
  • 1
  • 15
0

Got my answer http://groups.google.com/group/android-developers/browse_thread/thread/78fecbc156f4a1ea Its not possible.

hassanadnan
  • 435
  • 3
  • 8
  • 17
  • It IS possible, using the method you describe in your question. Have you remembered to set the correct permissions in your manifest? Take a look at your logcat output to identify where the system registers the incoming sms and find out why your receiver doesn't intercept it. – Marmoy Jul 13 '11 at 11:03
  • I have submitted a complete answer to clarify – Marmoy Jul 13 '11 at 11:04