Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this code</p> <p>In Broadcast Receiver</p> <pre><code> public class SMSReceiver extends BroadcastReceiver{ String str = ""; String no = ""; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; if (bundle != null) { //---retrieve the SMS message received--- Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; for (int i=0; i&lt;msgs.length; i++){ msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); str += msgs[i].getMessageBody().toString(); str += "\n"; no = msgs[i].getOriginatingAddress(); //Resolving the contact name from the contacts. Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(no)); Cursor c = context.getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME},null,null,null); try { c.moveToFirst(); String displayName = c.getString(0); String ContactName = displayName; Toast.makeText(context, ContactName, Toast.LENGTH_LONG).show(); } catch (Exception e) { // TODO: handle exception }finally{ c.close(); } } } } } </code></pre> <p>You have to register this broadcast receiver in the manifest with intentfilter</p> <pre><code> &lt;receiver android:name="SMSReceiver"&gt; &lt;intent-filter&gt; &lt;action android:name= "android.provider.Telephony.SMS_RECEIVED" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; </code></pre> <p>ALso you have to add user permission like</p> <pre><code>&lt;uses-permission android:name="android.permission.SEND_SMS"/&gt; &lt;uses-permission android:name="android.permission.RECEIVE_SMS"/&gt; &lt;uses-permission android:name="android.permission.READ_CONTACTS"/&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload