0

I tried this code as being mentioned in this post:http://stackoverflow.com/questions/7089313/android-listen-for-incoming-sms-messages

But what seems to be the problem is that, its not working when I installed the program in my phone. I want this application run at the background. When I checked my phone running processes, it's not there.

By the way, the codes below are the online codes that I have for this application. Is this correct?

Thanks

RJ

public class SmsListener extends BroadcastReceiver{

private SharedPreferences preferences;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
        Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
        SmsMessage[] msgs = null;
        String msg_from;
        if (bundle != null){
            //---retrieve the SMS message received---
            try{
                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]);
                    msg_from = msgs[i].getOriginatingAddress();
                    String msgBody = msgs[i].getMessageBody();
                }
            }catch(Exception e){
                Log.d("Exception caught",e.getMessage());
            }
        }
    }
}
}

Note: In your manifest file add the BroadcastReceiver-

<receiver android:name=".listener.SmsListener">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

Add this permission:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
user1120260
  • 375
  • 4
  • 12
  • 29
  • It may not run in the background if the service is started on SMS receive. Have you tried texting the device to see if it picks it up? – Cat Sep 04 '12 at 03:14
  • You need to start the app atleast once, before broadcastreciever starts working. Check this [post](http://stackoverflow.com/a/12255118/1109425) – nandeesh Sep 04 '12 at 03:14
  • I tried to run the app, but suddenly after a few seconds it terminates.. Yes, I tried to send a message in to my num. but nothing happens... – user1120260 Sep 04 '12 at 03:39
  • Have you tried debugging your code...?? – Name is Nilay Nov 05 '12 at 09:44

0 Answers0