Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't change the received intent, but if you're... hehe... <em>intending</em> to modify a received SMS, because it's an ordered broadcast, you can stop the broadcast, modify the intent's extras, and then rebroadcast it:</p> <p>Add the following to your ApplicationManifest.xml before the Application tag:</p> <pre><code>&lt;uses-permission android:name="android.permission.BROADCAST_SMS" /&gt; </code></pre> <p>Note that this will appear on your app's permissions page in the Play Store. Be prepared to explain it.</p> <p>And add this to the Intent-Filter tag in your Receiver:</p> <pre><code>android:priority="2147483647" </code></pre> <p>That's the highest possible integer in Java, ensuring that your receiver gets first priority. Note that receivers in other applications (notably GO SMS) might be using this same strategy, which may mean that the two receivers end up fighting over a message. If this happens, you'll probably have to uninstall the other app.</p> <p>And then in your receiver:</p> <pre><code>public void onReceive(Context context, Intent intent){ //Messages are delivered as extras in the intent Bundle bundle = intent.getExtras(); //If this intent was rebroadcasted already, ignore it. if(bundle.getBooleanExtra("rebroadcast", false) return; //Abort the broadcast abortBroadcast(); //Some examples use regular arrays, // but I prefer a more sophisticated solution. ArrayList&lt;SmsMessage&gt; msgs = new ArrayList&lt;SmsMessage&gt;(); //Check to make sure we actually have a message if(bundle != null){ //Get the SMS messages //They are delivered in a raw format, //called Protocol Description Units or PDUs //Messages can sometimes be delivered in bulk, //so that's why it's an array Object[] pdus = (Object[]) bundle.get("pdus"); //I prefer the enhanced for-loop. Why reinvent the wheel? for(Object pdu : pdus) msgs.add(SmsMessage.createFromPdu((byte[])pdu)); //Do stuff with the message. ArrayList&lt;SmsMessage&gt; newMessages = smsReceived(msgs); //Convert back to PDUs ArrayList&lt;Object&gt; pdus; Iterator&lt;SmsMessage&gt; iterator = newMessages.iterator(); //Iterate over the messages and convert them while(iterator.hasNext()){ pdus.add(iterator.next().getPdu()) } //Convert back to an Object[] for bundling Object[] pduArr = pdus.toArray(); //Redefine the intent. //It already has the broadcast action on it, //so we just need to update the extras bundle.putExtra("pdus", pduArr); //Set a rebroadcast indicator so we don't process it again //and create an infinite loop bundle.putExtra("rebroadcast", true); intent.putExtras(bundle); //Finally, broadcast the modified intent with the original permission sendOrderedBroadcast(intent, "android.permission.RECEIVE_SMS"); else{ //For some reason, there was no message in the message. //What do you plan to do about that? I'd just ignore it. } } </code></pre> <p>While this is a functional example, I would recommend doing most of this in a new Thread so as to not block the UI thread if your message handling is processor-intensive. For example, I did it in mine because I employ a lot of regex to manipulate the message, as well as perform some pretty heavy database queries in relation to the message.</p> <p>If I did something wrong or could have done better, please don't hesitate to correct me. Most of this code is in my own project and I'd like to be able to guarantee its functionality.</p> <p>Have a nice day.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload