I am using the SMS Manager class to send SMS through my code. The code works well in single sim phones but in some dual SIM phones, it returns me GENERIC FAILURE. I search a lot for this but found no proper solution for this. Please help me out. my code is as,
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
"MY_SMS_SENT"), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent("MY_SMS_DELIVERED"), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, messageBody, sentPI, deliveredPI);
and my receiver is as,
#Override
public void onReceive(Context context, Intent intent) {
//errorCode
if(intent.getAction().equals("MY_SMS_SENT")) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(context);
boolean isSIM1Ready = telephonyInfo.isSIM1Ready();
boolean isSIM2Ready = telephonyInfo.isSIM2Ready();
boolean isDualSIM = telephonyInfo.isDualSIM();
Log.e("IS DUAL SIM", "" + isDualSIM);
Log.e("isSIM1Ready", "" + isSIM1Ready);
Log.e("isSIM2Ready", "" + isSIM2Ready);
if(isDualSIM) {
if(isSIM1Ready || isSIM2Ready) {
Toast.makeText(context, "An Unexpected failure occured while sending SMS. Please check whether you have working SMS plan and try again later.",
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context, "Please activate atleast one SIM for sending SMS and retry.",
Toast.LENGTH_LONG).show();
}
}
else {
if(!isSIM1Ready) {
Toast.makeText(context, "No SIM detected to perform this action.",
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context, "An Unexpected failure occured while sending SMS. Please check whether you have working SMS plan and try again later.",
Toast.LENGTH_LONG).show();
}
}
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "No service available. Please try again later.",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context, "SMS not sent. Please try again later.",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "SMS Not Sent. Please try again later.",
Toast.LENGTH_SHORT).show();
break;
}
}
else if(intent.getAction().equals("MY_SMS_DELIVERED")) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Log.d("SMS STATUS", "SMS delivered");
/* Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();*/
break;
case Activity.RESULT_CANCELED:
Log.d("SMS STATUS", "SMS not delivered");
/*Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();*/
break;
}
}
}
Related
I'm creating an app where part of it will send a message if it meet a certain condition but whenever it is being sent, the receiver does not call onReceive method. I tried all the suggestions from the answered SO questions but nothing helped me fix it. I want to get the report if my SMS is sent with these codes:
BroadcastReceiver class:
public class SendSMS extends BroadcastReceiver {
public SendSMS() {
super();
}
#Override
public void onReceive(Context context, Intent intent) {
int resultCode = getResultCode();
switch (resultCode) {
case AppCompatActivity.RESULT_OK:
result = "Message sent.";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
result = "Error. Message not sent.";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
result = "Error: No service.";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
result = "Error: Null PDU.";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
result = "Error: Radio off.";
break;
}
}
}
Sending text class:
public class SMSSender {
private Context context;
private final int MAX_SMS_MESSAGE_LENGTH = 160;
private final String SMS_SENT = "sent";
private final String SMS_DELIVERED = "delivered";
public SMSSender(Context context) {
this.context = context;
}
public void sendSMS(String phoneNumber, String message) {
SendSMS brSendSMS = new SendSMS();
try {
//##### Also tried all kinds of context ###### //
PendingIntent piSend = PendingIntent.getBroadcast(context.getApplicationContext(), 0, new Intent(SMS_SENT), 0);
int length = message.length();
context.getApplicationContext().registerReceiver(brSendSMS, new IntentFilter(SMS_SENT));
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, piSend, null);
} catch (Exception ex) {
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
} finally {
context.getApplicationContext().unregisterReceiver(brSendSMS);
}
}
}
And to test this, I placed it in button clicked method.
SMSSender smsSender = new SMSSender(this);
smsSender.sendSMS("000000", "TESTING ONLY");
What am I doing wrong here? I also learned that it uses my default sms app stating "Not sent. Tap to try again".
What I want is... when an incoming call is coming it will answered automatically.
Here is my code but its not working.
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Incoming Call State", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "Ringing State Number is -" + incomingNumber, Toast.LENGTH_SHORT).show();
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))) {
Toast.makeText(context, "Call Received State", Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Toast.makeText(context, "Call Idle State", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
I hope in the RINGING function I need to do some code. But I am not getting the proper ans so I post this. Please Developers Help me out.
Thanks in advance
I am working on an app which determine a call is incoming or outgoing call. Not understanding why CALL_STATE_OFFHOOK is calling two times during a call.Any help please
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
startedCall=false;
log.info("state_idle");
// log.info("onCallStateChanged CALL_STATE_IDLE");
/* Intent idle = new Intent(context, BackgroundServices.class);
context.stopService(idle);*/
// Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
log.info("onCallStateChanged CALL_STATE_OFFHOOK");
if(!startedCall){
log.info("it is a outgoing call");
}
if (startedCall){
log.info("it is a incoming call");
}
/* Intent offHook = new Intent(context, BackgroundServices.class);
context.startService(offHook);*/
//Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
startedCall = true;
log.info("call state ringing");
//Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
break;
}
}`
I'm using https://github.com/Pyo25/phonegap-sms-sending-plugin to send SMS and it sends fine, however, even if there is no network coverage/ flight mode the plugin still returns me status.OK though the SMS is not sent.
Is there anyway to assure or check the message status?
I have tried registering a broadcast receiver in my sendSMS java but it doesn't register
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(smsSent));
this works GREAT! it has a sent and delivery intent
http://androidopera.blogspot.com/2013/03/smsplugin-phonegap-android.html#comment-form
I have designed my own compose screen for sending sms,
in that when send button is clicked
I am sending msg to multiple recepients
sendBTN.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
avedPhoneno = "5558,5554,5556";
StringTokenizer st = new StringTokenizer(savedPhoneno, ",");
while (st.hasMoreElements()) {
tempMobileNumber = (String) st.nextElement();
System.out.println("tempMobileNumber in onclicklistener "+tempMobileNumber);
if (tempMobileNumber.length() > 0
&& encryptedText.toString().trim().length() > 0) {
sendSMS(tempMobileNumber,
base64msgString.concat(timeStamp1));
} else {
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}
and the method for sending sms is
private void sendSMS(String edittextSmsNumber, String msg) {
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
PendingIntent DeliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
// short port = 8888;
sms.sendTextMessage(edittextSmsNumber, null, msg, sentPI, DeliveredPI);
}
and in the broadcast reciever i am trying to store the data into database
private BroadcastReceiver sendreceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
String info = "Send information: ";
switch(getResultCode())
{
case Activity.RESULT_OK:
info += "send successful";
smsSent = true;
if (senderinfo != null && timeStamp != 0 && msgString != null) {
System.out.println("entered into database");
System.out.println("tempMobileNumber is "
+ tempMobileNumber);
outboxDatabase.open();
outboxDatabase.insertintoOutbox(msgString, key,
tempMobileNumber, timeStamp);
outboxDatabase.close();
}
//unregisterReceiver(this);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
info += "send failed, generic failure";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
info += "send failed, no service";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
info += "send failed, null pdu";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
info += "send failed, radio is off";
break;
}
Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
}
};
but I am able to store the last number i.e., 5556 only in the database thrice.
I want to store all the three numbers into the database.
I want to store when only the msg is sent properly
i.e., when getResultCode() = Activity.RESULT_OK;
able to send to the three numbers but only not able to store in the database
how to overcome this.