My Android application is sending SMS and it's working fine. However, when the user enters a wrong number, the SMS is sent without error message. Therefore, the user doesn't know that he entered a wrong number. How can I fix that?
Activity.java
StringTokenizer st=new StringTokenizer(phoneNo,",");
while (st.hasMoreElements())
{
String tempMobileNumber = (String)st.nextElement();
if(tempMobileNumber.length()>0 && sms.trim().length()>0)
{
sendSMS(tempMobileNumber, sms);
}
else
{
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}
private void sendSMS(String phoneNumber, 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:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
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);
}
You Need to validate whether User Input is phone Number or not
For Example:Mobile No must be 10 digit
String number="2525252212"
Pattern mobileNo= Pattern.compile("\\d{10}");
Matcher matcher = mobileNo.matcher(number);
if (matcher.matches()) {
//go on
} else {
//Show Dialog
}
There's a library i use for that : libphonenumber https://code.google.com/p/libphonenumber/
There are some number verification methods in there. Unless you want to implement the verifications manually.
Related
I have following code and I want to know which sms has been delivered or not.
My code sends 1-5 sms every 30 seconds, so when toast "sms not delivered" appears, I do not know which one was not delivered. I don't know if this is the right way to do this but It was the most common solution to this problem
public boolean sendSMS(String id, String num, String msg) {
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);
registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
registerReceiver(deliveryBroadcastReciever, new
IntentFilter(DELIVERED));
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("xxxxxxxx", null, msg, sentPI, deliveredPI);
Log.e("Message Sent", num + " " + msg + " " + id);
return true;
}
class DeliverReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, 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;
}
}
}
class SentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "sms sent", Toast.LENGTH_SHORT)
.show();
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;
}
}
I'm sorry for my bad English.
EDIT:
public boolean sendSMS(String id, String num, String msg) {
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);
registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));
smsManager.sendTextMessage("+xxxxxxxxxx", null, msg, sentPI, deliveredPI);
Log.e("Message Sent", num + " " + msg + " " + id);
return true;
}
public class DeliverReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null){
final Object[] bObject = (Object[]) bundle.get("pdus");
for (int i = 0; i < bObject.length; i++){
SmsMessage current = SmsMessage.createFromPdu((byte[]) bObject[i]);
String phoneNum = current.getDisplayOriginatingAddress();
}
}
}catch (Exception e){
Log.e("Deliver Reciever",e.toString());
}
}
}
Debug screenshot: http://imgur.com/a/GulgP
Firstly, make your SmSManager a static class object. Then you can get the current phone number/phone numbers from the PDU that is hiddent inside intent object.
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
String phone = "";
SmsMessage smsMessage;
try {
if (bundle != null) {
final Object[] pdu = (Object[]) bundle.get("pdu");
for (int i = 0; i < pdu.length; i++) {
smsMessage = SmsMessage.createFromPdu((byte[]) pdu[i]);
phone = current.getDisplayOriginatingAddress();
}
}
}
}
I am a newcomer to androi. My problem is the following I have a code that sends text messages all right but I would like to be able to do that when the sending of the message fails automatically try to send it again about 3 times after that if it is not sent since it does nothing.
Greetings I hope you can help me I had a good time with this problem.
Here I leave the code.
public class Principal extends Activity {
EditText txtPhone;
EditText txtMsg;
Button btnSend;
TextView msj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
txtPhone = ((EditText)findViewById(R.id.txtPhone ));
txtMsg = ((EditText)findViewById(R.id.txtMsg ));
btnSend = ((Button)findViewById(R.id.btnSend ));
msj = ((TextView)findViewById(R.id.txtmsj));
btnSend.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
PendingIntent sentIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("SMS_SENT"), 0);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case Activity.RESULT_OK:
msj.setText("SMS SENT");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
msj.setText("I can not send SMS");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
msj.setText("service not available");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
msj.setText("PDU (Protocol Data Unit) es NULL");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
msj.setText("turn on the radio");
break;
}
}
}, new IntentFilter("SMS_SENT"));
SmsManager sms = SmsManager.getDefault();
if( txtPhone.getText().toString().length()> 0 &&
txtMsg.getText().toString().length()>0 )
{
sms.sendTextMessage( txtPhone.getText().toString() , null, txtMsg.getText().toString() , sentIntent, null);
}
else
{
Toast.makeText(getApplicationContext(), "Can not send, data is incorrect", Toast.LENGTH_SHORT).show();
}
}
});
}
I am trying to develop sms app that can load messages and tel numbers from server and store it internally, therefore when trying to send all messages at once using loop only some of them are sent, and this is my code. please help
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
String SENT = "sent";
String DELIVERED = "delivered";
Intent sentIntent = new Intent(SENT);
/*Create Pending Intents*/
PendingIntent sentPI = PendingIntent.getBroadcast(
getApplicationContext(), 0, sentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent deliveryIntent = new Intent(DELIVERED);
PendingIntent deliverPI = PendingIntent.getBroadcast(
getApplicationContext(), 0, deliveryIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
/* Register for SMS send action */
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String result = "";
switch (getResultCode()) {
case Activity.RESULT_OK:
result = "Transmission successful";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
result = "Transmission failed";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
result = "Radio off";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
result = "No PDU defined";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
result = "No service";
break;
}
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}, new IntentFilter(SENT));
/* Register for Delivery event */
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Delivered",
Toast.LENGTH_LONG).show();
}
}, new IntentFilter(DELIVERED));
/*Send SMS*/
ArrayList<Messages> messagesArrayList = new ArrayList<>();
/* loading messages and phone into arraylist */
messagesArrayList = db.getMessages();
for (int i = 0; i < messagesArrayList.size() ; i++) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("090" + messagesArrayList.get(i).getPhone(), null, messagesArrayList.get(i).getBody(), sentPI,
deliverPI);
}
}catch(Exception ex){
Toast.makeText(getApplicationContext(),
ex.getMessage().toString(), Toast.LENGTH_LONG)
.show();
ex.printStackTrace();
}
}
});
use this
String strnum="100864445556;1004522145586;100855822557;100885222559";
Uri smsToUri = Uri.parse("smsto:" + strnum);
if you are getting numbers from database query then
with the help of loops
make string strnum = num1 + ";" + num2 + ";" + num3 + ";" so on
This function will call another function (prince.Encrypt) that encrypts (PRINCE algorithm) the text message. But once it is linked with the encryption function, sending SMS fails. The function was working fine before encryption was included.
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v, String args)
{
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
{
LongBuffer messageBuf = TooLong.messageToLongBuffer(message);
messageBuf.flip();
long[] messageData = new long[messageBuf.remaining()];
LongBuffer i = messageBuf.get(messageData);
String v1=prince.Encrypt(i, k0, kop, k1, t);
sendSMS(phoneNo, v1);
}
else
Toast.makeText(getBaseContext(),"Please enter both phone number and message.",Toast.LENGTH_SHORT).show();
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
}
//---sends a SMS message to another device---
private void sendSMS(String phoneNumber, String v1)
{
/*
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, test.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
*/
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:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
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, v1, sentPI, deliveredPI);
}
}
I made sms application, it's running well if the message length < 160, but, when message length is more than 160, the emulator said that the application force close,
How I fix that?
here is my code..
private void kirimSMS(String string1, String string2) {
pi1 = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
pi2 = PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"),
0);
sendBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(KirimSMS.this.getBaseContext(),
"SMS terkirim", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(KirimSMS.this.getBaseContext(),
"Gagal Kirim", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(KirimSMS.this.getBaseContext(),
"No Services", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(KirimSMS.this.getBaseContext(), "No PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(KirimSMS.this.getBaseContext(), "Radio Off",
Toast.LENGTH_SHORT).show();
break;
}
}};
deliveryBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(KirimSMS.this.getBaseContext(),
"Pesan terkirim", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(KirimSMS.this.getBaseContext(),
"Pesan Tidak terkirim", Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
//SmsManager.getDefault().sendTextMessage(string1, null, string2, pi1,pi2);
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(stringIsiPesan);
smsManager.sendMultipartTextMessage(stringNoHp, null, parts, null, null);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnEnkripsi:
if (cekField()) {
String kunci = null;
String hasil = null;
if (MainActivity.DEFAULT_KEY.equals(""))
kunci = "ABCD";
else
kunci = MainActivity.DEFAULT_KEY;
Kriptoku enktripsi = new Kriptoku();
hasil = enktripsi.enkripsibaru(txtPesan.getText().toString(),kunci);
txtPesan.setText(hasil);
btnEnkripsi.setEnabled(false);
} else {
AlertDialog.Builder loBuilder2 = new AlertDialog.Builder(this);loBuilder2
.setMessage("lengkapi data..")
.setTitle("Oo..Oo..")
.setPositiveButton("OK",
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
});
loBuilder2.create();
loBuilder2.show();
}
break;
case R.id.btnKirimPesan:
//if (txtPesan.length() <= 160) {
if (cekField()) {
kirimSMS(this.txtNoHp.getText().toString(), this.txtPesan.getText().toString());
dao = SQLiteDAO.getInstance(this,
new Class[] { Outbox.class });
Outbox localSMS = new Outbox();
localSMS.setNoHp(this.txtNoHp.getText().toString());
localSMS.setPesan(this.txtPesan.getText().toString());
SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("HH:mm, dd MMM yyyy");
Calendar localCalendar = Calendar.getInstance();
localSMS.setTime(localSimpleDateFormat.format(localCalendar.getTime()));
dao.insert(localSMS);
finish();
} else {
AlertDialog.Builder loBuilder1 = new AlertDialog.Builder(this);
loBuilder1
.setMessage("Lengkapi Data")
.setTitle("Aduuhh...")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick{ DialogInterface dialog,int which) {
//TODO Auto-generated method stub
}
});
loBuilder1.create();
loBuilder1.show();
}
} //else {
// Toast.makeText(this, String.valueOf("Karakter lebih dari 160"), Toast.LENGTH_LONG).show();
//return;
// break;
}
please, help me
SMS = Short Message Service, where 'short' is 160 characters or less. If you want to send more, break the message into two, or more, parts.
by default the max length of an SMS can be only 160 characters
that what SHORT means in SMS = "SHORT MESSAGING SERVICE"
if you still want to send more than 160 characters , you need to split it into two or convert it into MMS
so use an if condition to check if the length is greater than 160 characters