I'm trying to write an app that sends SMS messages.
To do this, I use the following code:
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("sms:" + number));
smsIntent.putExtra("sms_body", message);
smsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(smsIntent);
The problem is: when I run it, it shows me a dialog and I need to choose with which app I want to send the message (WhatsApp/Hangouts/Messaging/etc.) and when I choose "Messaging", it only prepares the message and waits for me to press "send".
How can I send the message immediately through "Messaging" (without the dialog and without waiting until "send" is pressed)?
try to use SmsManager API
String phoneNo = "123456";
String msg= "Hello";
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// send the message
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
add this to your manifest :
<uses-permission android:name="android.permission.SEND_SMS" />
Related
I have tried this code but I did not receive SMS in my phone.
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
//Get the SmsManager instance and call the sendTextMessage method to send message
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage("88xxxxxxx0", null, "hello javatpoint", pi,null);
You can send message from your application through this:
public void sendSMS(String MoNo, String msg) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(MoNo, 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 message
<uses-permission android:name="android.permission.SEND_SMS" />
Thanks. Happy Coding...
Is there any way to send a text message without getting a confirmation? My current code for this is:
String phoneNumber = "1234567";
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + phoneNumber)); // This ensures only SMS apps respond
intent.putExtra("sms_body", "TESTING TEXT MESSAGE! IT WORKS!");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Yes you should use SmsManager. Here is an example:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
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?
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" />
public void sendLongSMS() {
String phoneNumber = "0123456789";
String message = "Hello World! Now we are going to demonstrate " +
"how to send a message with more than 160 characters from your Android application.";
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
}
and don't forget to add
<uses-permission android:name="android.permission.SEND_SMS"/>
Sending sms with permission request :
Add In manifest :
<uses-permission android:name="android.permission.SEND_SMS" />
Add Java Function :
void sendSmsMsgFnc(String mblNumVar, String smsMsgVar)
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED)
{
try
{
SmsManager smsMgrVar = SmsManager.getDefault();
smsMgrVar.sendTextMessage(mblNumVar, null, smsMsgVar, null, null);
Toast.makeText(getApplicationContext(), "Message Sent",
Toast.LENGTH_LONG).show();
}
catch (Exception ErrVar)
{
Toast.makeText(getApplicationContext(),ErrVar.getMessage().toString(),
Toast.LENGTH_LONG).show();
ErrVar.printStackTrace();
}
}
else
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(new String[]{Manifest.permission.SEND_SMS}, 10);
}
}
}
Yes, found the answer to my own question :)
Use the following code for the same :
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(srcNumber, null, message, null, null);
This requires the following permission to be declared on the android manifest xml.
<uses-permission android:name="android.permission.SEND_SMS"/>
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
I am trying to use the android share intent from my application.
I have listed all the contacts from my contacts content provider in my application.
Now i want to send message to all the contacts i have selected(in my app) using any of the message app
installed in user phone.
I do not want to user smsmaanger , simply want to user any sms sending application in user mobile if
available.
I tried to do with email works great but not with sms .
I tried with email as works great
public static void send(Context ctx, String[] addy, String subject,
String body,File attachment) {
try {
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("message/rfc822");
sendIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
addy);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
//sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
ctx.startActivity(Intent.createChooser(sendIntent,
"Send via which Application?"));
} catch (Exception e) {
Toast.makeText(ctx, "No activity was found to handle this action",
Toast.LENGTH_SHORT).show();
}
}
For sms i am using like this .
public static void send(Context ctx, String addy, String subject,
String body,File attachment) {
try {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("vnd.android-dir/mms-sms");
sendIntent.putExtra(android.content.Intent.EXTRA_PHONE_NUMBER,
addy);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
//sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
ctx.startActivity(Intent.createChooser(sendIntent,
"Send via which Application?"));
} catch (Exception e) {
Toast.makeText(ctx, "No activity was found to handle this action",
Toast.LENGTH_SHORT).show();
}
}
I just simply want to add my all contacts to users message app for sending message, with a message
possible
To send SMS to multiple numbers you need to separate the numbers with ;
Sample here:
String toNumbers = "";
ArrayList<String> numbersArrayList;// your phone numbers here
for ( String number : numbersArrayList)
{
toNumbers = toNumbers + number + ";"//separating numbers with semicolon
}
toNumbers = toNumbers.subString(0, toNumbers.length - 1);// remove the last semicolon
...
sendIntent.putExtra(android.content.Intent.EXTRA_PHONE_NUMBER, toNumbers);
I have use SmsManager.getDefault().sendTextMessage but not send sms i want to method programmatically send sms Please see this code
EDIT CODE:
public void sendEmail(View view){
if (hasConnection() == false){
Intent goPop = new Intent(getApplicationContext(),ShowPopup.class);
startActivity(goPop);
finish();
}
statusOfGPS = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Set Email option
final Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",OpsEmail, null));
//Check Send SMS
if (mSMSCheckbox.isChecked()){
SMS = true;
}else{
SMS = false;
}
//Check GPs
if (statusOfGPS == true){
LocationBy = "GPS";
}else{
LocationBy = "INTERNET";
}
setUpAddress();
//SMS true
if (mLocationClient != null && mLocationClient.isConnected() && SMS == true) {
//set massage to send
DescribeText = "This is a Emergency message \n\n PLEASE HELP ME!!! at\n\n "+
addressText+ "\n Location("+LocationBy+"):("+mLocationClient.getLastLocation().getLatitude() + mLocationClient.getLastLocation().getLongitude()+
") \n\n Please check maps <a href='https://maps.google.com/maps?q="+mLocationClient.getLastLocation().getLatitude()+","+mLocationClient.getLastLocation().getLongitude()+"&ll="+mLocationClient.getLastLocation().getLatitude()+","+mLocationClient.getLastLocation().getLongitude()+"&z=17'>here</a>";
//send SMS First
String sent = "android.telephony.SmsManager.STATUS_ON_ICC_SENT";
SmsManager smsManager = SmsManager.getDefault();
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(sent), 0);
smsManager.sendTextMessage(OpsPhoneNumber, null, DescribeText, pi,null);
//send email
i.putExtra(Intent.EXTRA_SUBJECT, "Emergency Location");
i.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(DescribeText));
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_LONG).show();
}
Log.d(TAG, "gps =" + statusOfGPS);
}
}
Next line of //send SMS First
i use
SmsManager.getDefault().sendTextMessage(OpsPhoneNumber, null,
DescribeText, null,null);
and in my manifest file i have:
<uses-permission android:name="android.permission.SEND_SMS"/>
My device to test is android 4.2.2 and 4.1.1
but apps don't send sms how to fix this?
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("", null, "< message body>", null, null);
SmsManager require, SMS_SEND permission in your android mainfest.