so I have a button that when clicked, I want to send a text message. To who, is specified by the editText. When I use the app, it says that the text is sent, however when I check in my messaging app it shows that nothing sent. How do I remedy this?
Example Number: "8667404531" (This is a bot hotline for example)
EditText editText = (EditText) findViewById(R.id.editText);
String myNum = editText.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(myNum, null, "Wake Up!", null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
First, make sure you have set this permission in your AndroidManifest.xml
:
<uses-permission android:name="android.permission.SEND_SMS"/>
Secondly, you should check self permission before you can send any messages. You can achieve that with checkSelfPermission()
call and corresponding onRequestPermissionsResult()
callback.
Check out this code below, it contains important parts related to requesting permissions from the device. This code below is a complete example of Activity
for sending SMS from your device:
public class MainActivity extends AppCompatActivity {
EditText number, text;
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number = (EditText) findViewById(R.id.editText);
text = (EditText) findViewById(R.id.editText2);
send = (Button) findViewById(R.id.button);
if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.SEND_SMS)){
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.SEND_SMS}, 1);
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.SEND_SMS}, 1);
}
} else {
//do nothing
}
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNumber = number.getText().toString();
String messageText = text.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, messageText, null, null);
Toast.makeText(MainActivity.this, "Message sent!", Toast.LENGTH_SHORT).show();
} catch (Exception e){
Toast.makeText(MainActivity.this, "Sending failed!", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case 1:{
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED){
Toast.makeText(MainActivity.this, "Permission granted!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Permission not granted!", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
}