How do I send a text message? It seems simple but it doesn't work for me.
I have the permission in the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.healthapp.healthapp">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application ...
I then have this code in my onClick:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("07123456789", null, "Hello there!", null, null);
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
But when I run this I get "Unfortunately App has stopped."
And the error message of:
FATAL EXCEPTION: main
Process: com.healthapp.healthapp, PID: 13477
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }
You've got two different methods of sending SMS in your code. If you want to use SmsManager
, then you don't need the Intent
/ startActivity()
method, which attempts to open another app to handle the SMS.
You can just remove everything after the smsManager.sendTextMessage()
line, and you won't get that Exception
anymore.
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("07123456789", null, "Hello there!", null, null);
If you want by Intent then
sendSmsByViewIntent()
Intent smsVIntent = new Intent(Intent.ACTION_VIEW);
// prompts only sms-mms clients
smsVIntent.setType("vnd.android-dir/mms-sms");
// extra fields for number and message respectively
smsVIntent.putExtra("address", phoneNumber.getText().toString());
smsVIntent.putExtra("sms_body", smsBody.getText().toString());
try{
startActivity(smsVIntent);
} catch (Exception ex) {
Toast.makeText(MainActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
Send by SmsManager then,
sendSmsByManager()
try {
// Get the default instance of the SmsManager
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber.getText().toString(),
null,
smsBody.getText().toString(),
null,
null);
Toast.makeText(getApplicationContext(), "Your sms has successfully sent!",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),"Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
send by ,
ACTION_SENDTO
// add the phone number in the data
Uri uri = Uri.parse("smsto:" + phoneNumber.getText().toString());
Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
// add the message at the sms_body extra field
smsSIntent.putExtra("sms_body", smsBody.getText().toString());
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(MainActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
should try any one these methods. but you are trying with two types in same time.
Finally main thing is permission at manifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.