+ Reply to Thread
Results 1 to 2 of 2

Thread: How to check for the incoming sms?

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    16

    How to check for the incoming sms?

    I want to develop an app which will check for incoming sms.One more thing is that the app should run in the background.

  2. #2
    Junior Member
    Join Date
    Aug 2012
    Posts
    15
    we have to defined BroadcastReceivers as private internal members to an Activity. In this case, it is probably best to define the receiver separately and register it in AndroidManifest.xml using the <receiver>tag. This will allow your receiver to
    process the incoming events even when your application is not active

    Code:
    public class SmsReceiver extends BroadcastReceiver { 
    private static final String SHORTCODE = "55443"; 
    @Override 
    
    public void onReceive(Context context, Intent intent) { 
    Bundle bundle = intent.getExtras(); 
    Object[] messages = (Object[])bundle.get("pdus"); 
    SmsMessage[] sms = new SmsMessage[messages.length]; 
    //Create messages for each incoming PDU 
    for(int n=0; n < messages.length; n++) { 
    sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]); 
    } 
    for(SmsMessage msg : sms) { 
    //Verify if the message came from our known sender 
    if(TextUtils.equals(msg.getOriginatingAddress(), SHORTCODE)) { 
    Toast.makeText(context, 
    "Received message from the mothership: "+msg.getMessageBody(), 
    Toast.LENGTH_SHORT).show(); 
    } 
    } 
    } 
    }
    AndroidManifest.xml
    Code:
    <?xml version="1.0" encoding="utf-8"?> 
    <manifest …> 
    <application …> 
    <receiver android:name=".SmsReceiver"> 
    <intent-filter> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
    </receiver> 
    </application> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    </manifest>

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts