Unable To Send Sms Using Smsmanager In Android
Solution 1:
I used following code to send sms to multiple numbers and sent sms gets saved in messages
privatevoidsendSMS(String phoneNumber, String message) {
StringSENT="SMS_SENT";
StringDELIVERED="SMS_DELIVERED";
PendingIntentsentPI= PendingIntent.getBroadcast(this, 0, newIntent(
SENT), 0);
PendingIntentdeliveredPI= PendingIntent.getBroadcast(this, 0,
newIntent(DELIVERED), 0);
// ---when the SMS has been sent---
registerReceiver(newBroadcastReceiver() {
@OverridepublicvoidonReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
ContentValuesvalues=newContentValues();
for (inti=0; i < MobNumber.size() - 1; i++) {
values.put("address", MobNumber.get(i).toString());// txtPhoneNo.getText().toString());
values.put("body", MessageText.getText().toString());
}
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
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;
}
}
}, newIntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(newBroadcastReceiver() {
@OverridepublicvoidonReceive(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;
}
}
}, newIntentFilter(DELIVERED));
SmsManagersms= SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
to send message to multiple numbers i used above function as :
if (MobNumber != null) {
for (inti=0; i < MobNumber.size(); i++) {
Stringmessage= MessageText.getText().toString();
StringtempMobileNumber= MobNumber.get(i).toString();
sendSMS(tempMobileNumber, message);
}
Solution 2:
1) add messages in Sent instead of Outbox, as Outbox contains messages which are suppose to send or in sending state.
2) when you send message add them at the same time in "content://sms/sent uri.
what is stopping u to store them in database. and what you tried yet.
use below code to sendSMS
smsManager.sendTextMessage(number, null,desc, null, null);
and by using content://sms/sent URI, you can insert the same text message into Message database
Solution 3:
If you are using dual sim device then you must have to mention the sender number, You can't pass null at that time. Otherwise, SmsManager will throw an error called SmsManager.RESULT_ERROR_GENERIC_FAILURE
.
Code to check numbers of active sims:
publicstaticintgetNumberOfActiveSim(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
SubscriptionManagersubscriptionManager= SubscriptionManager.from(context);
List<SubscriptionInfo> subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoList();
return subscriptionInfo != null ? subscriptionInfo.size() : 0;
}
TelephonyManagertelephonyManager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager != null && telephonyManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT){
return1;
}
return0;
}
Solution 4:
Hope this can help you.
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import android.view.View.OnClickListener;
import android.view.*;
publicclassMainActivityextendsActivityimplementsOnClickListener{
Button click;
EditText txt;
TextView txtvw;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button)findViewById(R.id.button);
txt = (EditText)findViewById(R.id.editText);
txtvw = (TextView)findViewById(R.id.textView1);
click.setOnClickListener(this);
}
@OverridepublicvoidonClick(View v){
txt.setText("");
v = this.getCurrentFocus();
try{
SmsManagersms= SmsManager.getDefault();
sms.sendTextMessage("8017891398",null,"Sent from Android",null,null);
}
catch(Exception e){
txtvw.setText("Message not sent!");
}
if(v != null){
InputMethodManagerimm= (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
}
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}
add this line in AndroidManifest.xml
<uses-permissionandroid:name="android.permission.SEND_SMS" />
Solution 5:
just send it directly... using the SmsManager
. Only problem is that is that the user won't know of it.
Post a Comment for "Unable To Send Sms Using Smsmanager In Android"