title: Android : java.lang.SecurityException: Permission Denial:
body: <p>I have an android application that i need to receive sms and I found a certain tutorial that teaches how to do that but when I run it  &amp; I get android permission exception</p>
  
  <blockquote>
    <p>FATAL EXCEPTION: main
                                                                                                           Process: androidreceivesms.javapapers.com.smsbroadcastreceiver, PID:
    12206
                                                                                                           java.lang.RuntimeException: Unable to start activity
    ComponentInfo{androidreceivesms.javapapers.com.smsbroadcastreceiver/androidreceivesms.javapapers.com.smsbroadcastreceiver.SmsActivity}:
    java.lang.SecurityException: Permission Denial: reading
    com.android.providers.telephony.SmsProvider uri content://sms/inbox
    from pid=12206, uid=10061 requires android.permission.READ_SMS, or
    grantUriPermission()
                                                                                                               at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                                               at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                                               at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                                               at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                                            Caused by: java.lang.SecurityException: Permission Denial: reading
    com.android.providers.telephony.SmsProvider uri content://sms/inbox
    from pid=12206, uid=10061 requires android.permission.READ_SMS, or
    grantUriPermission()
                                                                                                               at android.os.Parcel.readException(Parcel.java:1599)
                                                                                                               at
    android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                                                                                                               at
    android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                                                                                                               at
    android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                                                                                                               at android.content.ContentResolver.query(ContentResolver.java:491)
                                                                                                               at android.content.ContentResolver.query(ContentResolver.java:434)
                                                                                                               at
    androidreceivesms.javapapers.com.smsbroadcastreceiver.SmsActivity.refreshSmsInbox(SmsActivity.java:52)
                                                                                                               at
    androidreceivesms.javapapers.com.smsbroadcastreceiver.SmsActivity.onCreate(SmsActivity.java:47)
                                                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                                                               at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                                               at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                                               at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                                               at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                               at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)</p>
  </blockquote>
  
  <p><strong>Here is my AndroidManifest.xml file:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.WRITE_SMS" /&gt;
  &lt;uses-permission android:name="android.permission.READ_SMS" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_SMS" /&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity
          android:name=".SmsActivity"
          android:label="@string/app_name"
          android:theme="@style/AppTheme.NoActionBar"
          android:exported="true"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;receiver android:name=".SmsBroadcastReceiver" android:exported="true" &gt;
          &lt;intent-filter android:priority="999" &gt;
              &lt;action android:name="android.provider.Telephony.SMS_RECEIVED" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Here is my SmsActivity.java</strong></p>
  
  <pre><code>public class SmsActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
  
  private static SmsActivity inst;
  ArrayList&lt;String&gt; smsMessagesList = new ArrayList&lt;String&gt;();
  ListView smsListView;
  ArrayAdapter arrayAdapter;
  
  public static SmsActivity instance() {
      return inst;
  }
  
  @Override
  public void onStart() {
      super.onStart();
      inst = this;
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_sms);
      smsListView = (ListView) findViewById(R.id.SMSList);
      arrayAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, smsMessagesList);
      smsListView.setAdapter(arrayAdapter);
      smsListView.setOnItemClickListener(this);
  
      refreshSmsInbox();
  }
  
  public void refreshSmsInbox() {
      ContentResolver contentResolver = getContentResolver();
      Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
      int indexBody = smsInboxCursor.getColumnIndex("body");
      int indexAddress = smsInboxCursor.getColumnIndex("address");
      if (indexBody &lt; 0 || !smsInboxCursor.moveToFirst()) return;
      arrayAdapter.clear();
      do {
          String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
                  "\n" + smsInboxCursor.getString(indexBody) + "\n";
          arrayAdapter.add(str);
      } while (smsInboxCursor.moveToNext());
  }
  
  public void updateList(final String smsMessage) {
      arrayAdapter.insert(smsMessage, 0);
      arrayAdapter.notifyDataSetChanged();
  }
  
  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int pos, long id) {
      try {
          String[] smsMessages = smsMessagesList.get(pos).split("\n");
          String address = smsMessages[0];
          String smsMessage = "";
          for (int i = 1; i &lt; smsMessages.length; ++i) {
              smsMessage += smsMessages[i];
          }
  
          String smsMessageStr = address + "\n";
          smsMessageStr += smsMessage;
          Toast.makeText(this, smsMessageStr, Toast.LENGTH_SHORT).show();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Here is my SmsBroadcastReceiver.java</strong></p>
  
  <pre><code>public class SmsBroadcastReceiver extends BroadcastReceiver {
  public static final String SMS_BUNDLE = "pdus";
  
  public void onReceive(Context context, Intent intent) {
      Bundle intentExtras = intent.getExtras();
      if (intentExtras != null) {
          Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
          String smsMessageStr = "";
          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 += "SMS From: " + address + "\n";
              smsMessageStr += smsBody + "\n";
          }
          Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
  
          //this will update the UI with message
          SmsActivity inst = SmsActivity.instance();
          inst.updateList(smsMessageStr);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Can anyone please help me because i have been stack here for a long time now? Any help will be appreciated thanks</p>