title: Broadcast sms Reciever
body: <p>I am a newbie on android development and I'm trying to create an sms Reciever app but it is not working. I want the broadcast receiver to capture incoming messages and pass them to the Recycler view so that they can be displayed. At the moment nothing is being displayed.</p>
  <p>here is my code (Manifest File)</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;com.example.test1&quot;&gt;
  
      &lt;uses-permission android:name=&quot;android.permission.RECEIVE_SMS&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_SMS&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_SMS&quot; /&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.Test1&quot;&gt;
          &lt;receiver
              android:name=&quot;.SmsReceiver&quot;
              android:enabled=&quot;true&quot;
              android:exported=&quot;true&quot;&gt;&lt;/receiver&gt;
  
          &lt;activity
              android:name=&quot;.MainActivity&quot;
              android:exported=&quot;true&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;  
  </code></pre>
  <p>here is the MainActivity file</p>
  <pre><code> package com.example.test1;
      
      import android.content.ContentResolver;
      import android.content.pm.PackageManager;
      import android.database.Cursor;
      import android.net.Uri;
      import android.os.Bundle;
      
      import androidx.appcompat.app.AppCompatActivity;
      import androidx.core.app.ActivityCompat;
      import androidx.core.content.ContextCompat;
      import androidx.recyclerview.widget.LinearLayoutManager;
      import androidx.recyclerview.widget.RecyclerView;
      
      import java.util.ArrayList;
      
      public class MainActivity extends AppCompatActivity {
          //recycler view
          private RecyclerView recyclerView;
           private ArrayList&lt;SmsModel&gt;smsModel=new ArrayList&lt;&gt;();
          private static MainActivity inst;
          public static MainActivity instance() {
              return inst;
          }
      @Override
      public void onStart() {
          super.onStart();
          inst = this;
      }
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          recyclerView=findViewById(R.id.smsRecycler);
          ArrayAdapter adapter= new ArrayAdapter();
          adapter.setSmsModel(smsModel);
          recyclerView.setAdapter(adapter);
          recyclerView.setLayoutManager(new LinearLayoutManager(this));
  
          if(ContextCompat.checkSelfPermission(getBaseContext(), &quot;android.permission.READ_SMS&quot;) == PackageManager.PERMISSION_GRANTED) {
              refreshSmsInbox();
          }else {
              // Todo : Then Set Permission
              final int REQUEST_CODE_ASK_PERMISSIONS = 123;
              ActivityCompat.requestPermissions(MainActivity.this, new String[]{&quot;android.permission.READ_SMS&quot;}, REQUEST_CODE_ASK_PERMISSIONS);
          }
  
          }
      public void refreshSmsInbox() {
          ContentResolver contentResolver = getContentResolver();
          Cursor smsInboxCursor = contentResolver.query(Uri.parse(&quot;content://sms/inbox&quot;), null, null, null, null);
          int indexBody = smsInboxCursor.getColumnIndex(&quot;body&quot;);
          int indexAddress = smsInboxCursor.getColumnIndex(&quot;address&quot;);
          if (indexBody &lt; 0 || !smsInboxCursor.moveToFirst()) return;
          smsModel.clear();
          do {
              String str = &quot;SMS From: &quot; + smsInboxCursor.getString(indexAddress) +
                      &quot;\n&quot; + smsInboxCursor.getString(indexBody) + &quot;\n&quot;;
              smsModel.add((SmsModel) smsInboxCursor);
          } while (smsInboxCursor.moveToNext());
      }
  
      public void updateList(final String smsMessage) {
         
  
  
      }
  
  
  }
  </code></pre>
  <p>Here is my Recyclerview Adapter class</p>
  <pre><code> package com.example.test1;
      
      import android.content.Context;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.TextView;
      
      import androidx.annotation.NonNull;
      import androidx.recyclerview.widget.RecyclerView;
      
      import java.util.ArrayList;
      import java.util.List;
      
      public class ArrayAdapter extends RecyclerView.Adapter&lt;ArrayAdapter.viewHolder&gt; {
      
      
          private ArrayList&lt;SmsModel&gt;smsModel=new ArrayList&lt;&gt;();
      
          public ArrayAdapter() {
      
          }
      
          @NonNull
          @Override
          public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
              View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.smsview,parent,false);
              viewHolder viewHolder=new viewHolder(view);
              return viewHolder;
          }
      
          @Override
          public void onBindViewHolder(@NonNull viewHolder holder, int position) {
              holder.ContactsNO.setText(smsModel.get(position).getContactNumber());
              holder.SmsText.setText(smsModel.get(position).getContactSms());
              holder.Time.setText(smsModel.get(position).getTime());
              holder.Number.setText(smsModel.get(position).getNumber());
      
      
          }
      
          @Override
          public int getItemCount() {
              return smsModel.size();
          }
      
          public void setSmsModel(ArrayList&lt;SmsModel&gt; smsModel) {
              this.smsModel = smsModel;
              notifyDataSetChanged();
          }
      
          public class viewHolder extends RecyclerView.ViewHolder {
              private TextView ContactsNO;
              private TextView SmsText;
              private TextView Time;
              private TextView Number;
              public viewHolder(@NonNull View itemView) {
                  super(itemView);
                  ContactsNO=itemView.findViewById(R.id.contactsNo);
                  SmsText=itemView.findViewById(R.id.smsText);
                  Time=itemView.findViewById(R.id.time);
                  Number=itemView.findViewById(R.id.Number);
              }
          }
      }
  </code></pre>
  <p>And here is my Sms Receiver Class</p>
  <pre><code>package com.example.test1;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Build;
  import android.os.Bundle;
  import android.telephony.SmsMessage;
  import android.widget.Toast;
  
  import androidx.annotation.RequiresApi;
  
  public class SmsReceiver extends BroadcastReceiver {
      public static final String SMS_BUNDLE = &quot;pdus&quot;;
      @RequiresApi(api = Build.VERSION_CODES.M)
      @Override
      public void onReceive(Context context, Intent intent) {
          Bundle intentExtras = intent.getExtras();
          if (intentExtras != null) {
              Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
              String smsMessageStr = &quot;&quot;;
              for (int i = 0; i &lt; sms.length; ++i) {
                  SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
  
                  String smsBody = smsMessage.getMessageBody().toString();
                  String address = smsMessage.getOriginatingAddress();
  
                  smsMessageStr += &quot;SMS From: &quot; + address + &quot;\n&quot;;
                  smsMessageStr += smsBody + &quot;\n&quot;;
              }
              Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
  
              //this will update the UI with message
              MainActivity inst = MainActivity.instance();
              inst.updateList(smsMessageStr);
              }
          }
  
  
      }
  </code></pre>
  <p>here is my sms model class</p>
  <pre><code>package com.example.test1;
  
  public class SmsModel {
      private  String Id;
      private String ContactNumber;
      private String ContactSms;
      private String Time;
      private String Number;
  
      public SmsModel(String id, String contactNumber, String contactSms, String time, String number) {
          Id = id;
          ContactNumber = contactNumber;
          ContactSms = contactSms;
          Time = time;
          Number = &quot;Number&quot;;
      }
  
      public String getId() {
          return Id;
      }
  
      public void setId(String id) {
          Id = id;
      }
  
      public String getContactNumber() {
          return ContactNumber;
      }
  
      public void setContactNumber(String contactNumber) {
          ContactNumber = contactNumber;
      }
  
      public String getContactSms() {
          return ContactSms;
      }
  
      public void setContactSms(String contactSms) {
          ContactSms = contactSms;
      }
  
      public String getTime() {
          return Time;
      }
  
      public void setTime(String time) {
          Time = time;
      }
  
      public void setNumber(String number) {
          Number = number;
      }
  
      public String getNumber() {
          return Number;
      }
  
      @Override
      public String toString() {
          return &quot;SmsModel{&quot; +
                  &quot;Id='&quot; + Id + '\'' +
                  &quot;, ContactNumber='&quot; + ContactNumber + '\'' +
                  &quot;, ContactSms='&quot; + ContactSms + '\'' +
                  &quot;, Time='&quot; + Time + '\'' +
                  &quot;, Number='&quot; + Number + '\'' +
                  '}';
      }
  }
  </code></pre>
  <p>here is my main Activity code</p>
  <pre><code> &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
  
  &lt;TextView
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:id=&quot;@+id/sms&quot;
      android:text=&quot;SMS APP&quot;
      android:layout_centerHorizontal=&quot;true&quot;
      android:textFontWeight=&quot;600&quot;
      android:textStyle=&quot;bold&quot;/&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:layout_below=&quot;@+id/sms&quot;
          android:id=&quot;@+id/smsRecycler&quot;
  /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>and my layout resource file(smsview.xml)</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:padding=&quot;10dp&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/Number&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Number:&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/contactsNo&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginLeft=&quot;10dp&quot;
          android:layout_toRightOf=&quot;@+id/Number&quot;
          android:text=&quot;0700000000&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/smsText&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_below=&quot;@+id/Number&quot;
          android:layout_marginTop=&quot;10dp&quot;
          android:hint=&quot;sms text will apear here&quot; /&gt;
  
      &lt;TextView
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginLeft=&quot;200dp&quot;
          android:layout_toRightOf=&quot;@+id/contactsNo&quot;
          android:text=&quot;21:59&quot;
          android:id=&quot;@+id/time&quot;
          /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>i have tried anything i can but am not able to make it work.</p>