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>