Skip to content Skip to sidebar Skip to footer

How To Receive Sms Messages From A Specific Number?

I want to receive messages from a specific number. When I run the app, I get messages from everyone in my inbox. I got the source code from this site (android authority) package c

Solution 1:

Based on this tutorialsite(android authority)

In your BroadcastReceiver

int yourNumber=1234567890; // your specific numberStringaddress= smsMessage.getDisplayOriginatingAddress(); // incoming SMS numberif(address ==yourNumber){

  //display message
 }

Update

publicclassSmsBroadcastReceiverextendsBroadcastReceiver {
 String address;
publicstaticfinalStringSMS_BUNDLE="pdus";

publicvoidonReceive(Context context, Intent intent) {
    BundleintentExtras= intent.getExtras();

        if (intentExtras != null) {
        Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
        StringsmsMessageStr="";
        for (inti=0; i < sms.length; ++i) {
            Stringformat= intentExtras.getString("format");
            SmsMessagesmsMessage= SmsMessage.createFromPdu((byte[]) sms[i], format);

            StringsmsBody= smsMessage.getMessageBody().toString();
            address = smsMessage.getOriginatingAddress();


             smsMessageStr += "SMS From: " + address + "\n";
             smsMessageStr += smsBody + "\n";



        }
       if(address =="7911127456"){
        MainActivityinst= MainActivity.instance();
        inst.updateInbox(smsMessageStr);
      }
    }
}
}

update

publicvoidrefreshSmsInbox() {
         ContentResolvercontentResolver= getContentResolver();
         CursorsmsInboxCursor= 
    contentResolver.query(Uri.parse("content://sms/inbox"), null, null,
     null, null);
         intindexBody= smsInboxCursor.getColumnIndex("body");
         intindexAddress= smsInboxCursor.getColumnIndex("address");
         if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
         arrayAdapter.clear();

         do {
      if(indexAddress ==7911127456){
             Stringstr="SMS From: " + smsInboxCursor.getString(indexAddress) +
                     "\n" + smsInboxCursor.getString(indexBody) + "\n";
             arrayAdapter.add(str);
  }
         } while (smsInboxCursor.moveToNext()); 

  //messages.setSelection(arrayAdapter.getCount() - 1);
  arrayAdapter.notifyDataSetChanged();
 }

Post a Comment for "How To Receive Sms Messages From A Specific Number?"