Hi I'm working on an app that upon the click of a button sends the location of the person to a phone number of their choice by SMS. I've little experience and have looked into using Twilio API. Does anyone know if this would work or is there another way to go about this? Getting the location is already covered I'm just looking at how I can now send this to a phone number on the click of a button. Thanks
Asked
Active
Viewed 910 times
-1
-
How 'bout just sending it directly from the device? [Send SMS in android](http://stackoverflow.com/questions/4967448/send-sms-in-android) – Mike M. Apr 15 '17 at 14:10
-
Please be more careful tagging, this question is about Android programming, *not* Android Studio. – EJoshuaS - Stand with Ukraine Apr 15 '17 at 14:59
1 Answers
0
So, from what i know, you don't really need a 3rd party to send the SMS from Android to a predefined phone number.
Please check out this post, where the problem is tackled : Send SMS in android
For ease of use, you can use/check this function, that worked for my projects : *NOTE : this is for sending SMS to multiple phone numbers ( hence the for loop )
private void MultipleSMS(String phoneNumber, final String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
ContentValues values = new ContentValues();
for (int i = 0; i < MobNumber.size() - 1; i++) {
values.put("address", MobNumber.get(i).toString());
// txtPhoneNo.getText().toString());
values.put("body", message.toString());
}
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
doCall();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}