title: Android unable to send text to another device
body: <p>I have checked already posted solutions for the error which advised the I put in the permissions check.</p>
  
  <p>Using targetSdkVersion 26 - and Android Studio 3.2</p>
  
  <p>I set up (from a youtube tutorial) a sms within my application.  I have two apps on two separate devices.  I want the users from App A to be able to talk to the users of App B.  I put the same code in both apps from this tutorial</p>
  
  <p><a href="https://www.youtube.com/watch?v=dGnEEvCo1lM" rel="nofollow noreferrer">Sending and Receiving Messages or SMS - Android Studio Tutorial </a></p>
  
  <p>At first, when I run the app, I got an error:</p>
  
  <pre><code>Sending SMS Message: uid 10085 does not have android.permission.SEND_SMS
  </code></pre>
  
  <p>So, I added a permissions check in the button onclick()</p>
  
  <p>Now, when I run the apps, nothing happens when trying to send.</p>
  
  <p><strong>Manifest</strong></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.SEND_SMS" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_SMS" android:protectionLevel="signature" /&gt;
  
   &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity android:name=".Activities.RiderActivity"&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=".MessageReceiver"&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><strong>SMS_Activity</strong></p>
  
  <pre><code>private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          // display the message in the textView
          TextView inTxt = (TextView) findViewById(R.id.textMsg);
          inTxt.setText(intent.getExtras().getString("message"));
      }
  };
  
  
  btnSend.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String myMsg = tvMessage.getText().toString();
              String theNumber = tvNumber.getText().toString();
              if (ContextCompat.checkSelfPermission(SMS_Activity.this, Manifest.permission.SEND_SMS)
                      != PackageManager.PERMISSION_GRANTED) {
                  // Permission is not granted
              } else {
                  sendMsg(theNumber, myMsg);
              }
          }
      });
  
  
  protected void sendMsg(String theNumber, String myMsg) {
  
      String SENT = "Message Sent";
      String DELIVERED = "Message Delivered";
  
      PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new
              Intent(SENT), 0);
      PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
              new Intent(DELIVERED), 0);
  
      SmsManager sms = SmsManager.getDefault();
      sms.sendTextMessage(theNumber, null, myMsg, sentPI, null);
  }
  </code></pre>
  
  <h2>Edit</h2>
  
  <p><strong>Added onRequestPermissionsResult:</strong></p>
  
  <pre><code> @Override
  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
      switch (requestCode) {
          case MY_PERMISSIONS_REQUEST_SEND_SMS: {
              if (permissions[0].equalsIgnoreCase
                      (Manifest.permission.SEND_SMS)
                      &amp;&amp; grantResults[0] ==
                      PackageManager.PERMISSION_GRANTED) {
                  // Permission was granted.
              } else {
                  // Permission denied. Stop the app.
                  Log.d(TAG, getString(R.string.failure_permission));
                  Toast.makeText(this,
                          getString(R.string.failure_permission),
                          Toast.LENGTH_SHORT).show();
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Updated btnSend.setOnClickListener:</strong></p>
  
  <pre><code>btnSend.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String myMsg = tvMessage.getText().toString();
              String theNumber = tvNumber.getText().toString();
              if (ContextCompat.checkSelfPermission(SMS_Activity.this, Manifest.permission.SEND_SMS)
                      != PackageManager.PERMISSION_GRANTED) {
                  // Permission is not granted
                  ActivityCompat.requestPermissions(SMS_Activity.this,
                          new String[]{Manifest.permission.SEND_SMS}, 1);
                  sendMsg(theNumber, myMsg);
              }
          }
      });
  </code></pre>
  
  <h2>Edit #2</h2>
  
  <p>As recommended, I followed this tutorial:</p>
  
  <p><a href="https://google-developer-training.gitbooks.io/android-developer-phone-sms-course/content/Lesson%201/1_c_phone_calls.html" rel="nofollow noreferrer">Android Apps - Phone Calls and SMS</a></p>
  
  <p>giving me this error:</p>
  
  <pre><code>java.lang.IllegalStateException: Could not execute method for android:onClick
  Caused by: java.lang.SecurityException: Neither user 10085 nor current process has android.permission.READ_PHONE_STATE.
  </code></pre>
  
  <p>I then made these changes:</p>
  
  <p>AndroidManifest - added:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.READ_PHONE_STATE"/&gt;
  </code></pre>
  
  <p>SMSActivity - added:</p>
  
  <pre><code>private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 2;
  </code></pre>
  
  <p>... checkforPermissions() // runs at onCreate and before smsManager</p>
  
  <pre><code>if (ActivityCompat.checkSelfPermission(this,
              Manifest.permission.SEND_SMS) !=
              PackageManager.PERMISSION_GRANTED) {
          Log.d(TAG, getString(R.string.permission_not_granted));
  
          if (ActivityCompat.checkSelfPermission(this,
                  Manifest.permission.READ_PHONE_STATE) !=
                  PackageManager.PERMISSION_GRANTED) {
  
              Log.d(TAG, getString(R.string.permission_not_granted));
  
              ActivityCompat.requestPermissions(this,
                      new String[]{Manifest.permission.SEND_SMS},
                      MY_PERMISSIONS_REQUEST_SEND_SMS);
              ActivityCompat.requestPermissions(this,
                      new String[]{Manifest.permission.READ_PHONE_STATE},
                      MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
  
          }
  
      } else {
          // Permission already granted. Enable the SMS button.
          enableSmsButton();
      }
  </code></pre>
  
  <p>... onRequestPermissionsResult()</p>
  
  <pre><code>// Check if permission is granted or not for the request.
      switch (requestCode) {
          case MY_PERMISSIONS_REQUEST_SEND_SMS: {
              if (permissions[0].equalsIgnoreCase
                      (Manifest.permission.SEND_SMS)
                      &amp;&amp; grantResults[0] ==
                      PackageManager.PERMISSION_GRANTED) {
  
                  // Permission was granted. Enable sms button.
                  enableSmsButton();
  
              } else {
                  // Permission denied.
                  Log.d(TAG, getString(R.string.failure_permission));
                  Toast.makeText(this,
                          getString(R.string.failure_permission),
                          Toast.LENGTH_LONG).show();
                  // Disable the sms button.
                  disableSmsButton();
              }
          }
  
          break;
  
          case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
              if (permissions[0].equalsIgnoreCase
                      (Manifest.permission.READ_PHONE_STATE)
                      &amp;&amp; grantResults[0] ==
                      PackageManager.PERMISSION_GRANTED) {
  
                  // Permission was granted. Enable sms button.
                  enableSmsButton();
  
              } else {
                  // Permission denied.
                  Log.d(TAG, getString(R.string.failure_permission));
                  Toast.makeText(this,
                          getString(R.string.failure_permission),
                          Toast.LENGTH_LONG).show();
                  // Disable the sms button.
                  disableSmsButton();
              }
          }
  
          break;
  
          default:
              break;
      }
  </code></pre>
  
  <p>Now, when I run the apps, I get the same error as above.</p>
  
  <p><strong>SOLUTION RESOLVED</strong></p>
  
  <p>SMSACTIVITY</p>
  
  <pre><code>private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
  
  private boolean checkAndRequestPermissions() {
  
      int permissionSendSms = ContextCompat.checkSelfPermission(this,
              Manifest.permission.SEND_SMS);
      int permissionReceiveSms = ContextCompat.checkSelfPermission(this,
              Manifest.permission.RECEIVE_SMS);
      int permissionReadPhoneState = ContextCompat.checkSelfPermission(this,
              Manifest.permission.READ_PHONE_STATE);
  
      List&lt;String&gt; listPermissionsNeeded = new ArrayList&lt;&gt;();
      if (permissionSendSms != PackageManager.PERMISSION_GRANTED) {
          listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
      }
  
      if (permissionReceiveSms != PackageManager.PERMISSION_GRANTED) {
          listPermissionsNeeded.add(Manifest.permission.RECEIVE_SMS);
      }
  
      if (permissionReadPhoneState != PackageManager.PERMISSION_GRANTED) {
          listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
      }
  
      if (!listPermissionsNeeded.isEmpty()) {
          ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new
                  String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
          return false;
      }
      return true;
  }
  
  
  @Override
  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  
      switch(requestCode) {
          case REQUEST_ID_MULTIPLE_PERMISSIONS: {
              // Check if permission is granted or not for the request.
              if (grantResults.length == 0 || grantResults == null) {
                  /*If result is null*/
                  disableSmsButton();
  
              } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  /*If We accept permission*/
                  if (getIntent() != null) {
                      phone_no = getIntent().getStringExtra("phone");
                      Log.e(TAG, "phoneNo = " + phone_no);
  
  
                  }
  
                  enableSmsButton();
  
              } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                  /*If We Decline permission*/
                  disableSmsButton();
              }
          }
      }
  }
  </code></pre>
  
  <p>Everything works great now!!</p>