0

I'm currently coding for android and I want when I receive a SMS the app performs some operation... Now the strange part... I've teste the following code in the Emulator it works 100% but when I take to my android 2.3.3 with GO SMS it doesn't work at all :\

import android.content.BroadcastReceiver;
import android.content.Context;<br />
import android.content.Intent;<br />
import android.os.Bundle;<br />
import android.telephony.gsm.SmsMessage;<br />
import android.widget.Toast;


public class SMSReceiver extends BroadcastReceiver {
String lsms;
/* package */ static final String ACTION =
        "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
         //---get the SMS message passed in---        
        Bundle bundle = intent.getExtras();                
        SmsMessage[] msgs = null;        
        String str = "";                    
        if (bundle != null)        {           
            //---retrieve the SMS message received---            
            Object[] pdus = (Object[]) bundle.get("pdus");            
            msgs = new SmsMessage[pdus.length];                        
            for (int i=0; i<msgs.length; i++){                
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                                
                str += "SMS from " + msgs[i].getOriginatingAddress();                                     
                str += " :";                
                str += msgs[i].getMessageBody().toString();                
                str += "\n";                    
                }            
            //---display the new SMS message---            
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();        
            }

    }


}

And yes... I added the necessary code to AndroidManifest.xml The permission:

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

And the intent

<receiver android:name=".receiver.SMSReceiver" android:enabled="true">
      <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
      </intent-filter>
    </receiver>
tshepang
  • 12,111
  • 21
  • 91
  • 136
Pedro Cavaleiro
  • 835
  • 7
  • 21

1 Answers1

0

I've heard that GO SMS and other SMS apps intercept the SMS received broadcast and stop it getting through to anything else, so that only their notifications come up when you receive a text. You should try uninstalling GO SMS and see if it works then.

Unfortunately there isn't a way around the problem if that is what's wrong because they set their priority to the highest it can go, so nothing can receive the broadcast before it is destroyed.

Matt Harris
  • 3,496
  • 5
  • 32
  • 43
  • there is no other way? i read somewere that we could access through "content://sms/inbox" something like that... – Pedro Cavaleiro Jan 17 '12 at 16:39
  • Sorry, I meant there is no other way to receive the broadcast. You are right in saying there is a way around it. You should have a look into the ContentObserver class. Have a quick search on this site for it, there are some great examples. It is pretty straightforward. – Matt Harris Jan 17 '12 at 18:07