Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending SMS programmatically without opening message app

So far I am using the following code to send SMS to another phone through my app.

Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + srcNumber));                      intent.putExtra( "sms_body", message );                      startActivity(intent); 

However, this opens up the native messaging app, thereby putting my app's activity in the background. Is it possible to send the SMS directly without the native messaging app opening up? If yes, how?

like image 727
SoulRayder Avatar asked Oct 11 '14 04:10

SoulRayder


People also ask

How do I send SMS from app?

icon from the toolbar. Before starting your application, Android studio installer will display following window to select an option where you want to run your Android application. Now you can enter a desired mobile number and a text message to be sent on that number. Finally click on Send SMS button to send your SMS.

Can Android emulator send SMS?

No, you can not send SMS from an emulator to a cell phone, the simple and logical reason is that the emulators don't have a SIM Card.


1 Answers

You can send messages from your application through this:

public void sendSMS(String phoneNo, String msg) {     try {               SmsManager smsManager = SmsManager.getDefault();         smsManager.sendTextMessage(phoneNo, null, msg, null, null);             Toast.makeText(getApplicationContext(), "Message Sent",                 Toast.LENGTH_LONG).show();     } catch (Exception ex) {         Toast.makeText(getApplicationContext(),ex.getMessage().toString(),                 Toast.LENGTH_LONG).show();         ex.printStackTrace();     }  } 

Also, you need to give SEND_SMS permission in AndroidManifest.xml to send a message

<uses-permission android:name="android.permission.SEND_SMS" />

like image 165
Himanshu Agarwal Avatar answered Sep 28 '22 01:09

Himanshu Agarwal