Friday, November 16, 2012

Android Receive SMS Using Broadcast Receiver

 

The first are permissions . The application  must receive, from the database.   

Permissions can be set in Manifest.xml like:

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


Now our Activity class SMSreceiver is here...

private class SMSreceiver extends BroadcastReceiver
{
  private final String TAG = this.getClass().getSimpleName();
  @Override
  public void onReceive(Context context, Intent intent)
  {
     Bundle extras = intent.getExtras();
     String strMessage = "";
     if ( extras != null )
     {
        Object[] smsextras = (Object[]) extras.get( "pdus" );
        for ( int i = 0; i < smsextras.length; i++ )
        {
           SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);
           String strMsgBody = smsmsg.getMessageBody().toString();
           String strMsgSrc = smsmsg.getOriginatingAddress();  
           strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;                    
           Log.i(TAG, strMessage);
         }
      }

    }

}


No comments:

Post a Comment