Why Android.telephony.smsmanager Is Not Able To Send Message From Dialog Activity?
Here I have created an alert DialogActivity where 'setPositiveButton' builder, launch a simple dialog and try to send an android telephony sms message to the recipient. But mysteri
Solution 1:
Cheers! Problem got resolved.
There were no appropriate intantiation of the phone number field of the mSmsManager.sendTextMessage() method. Alas! how silly mistake i made which took some time to figure out. I believe i got mislead because of nesting of onClickListener() methods. He he...
I am reposting the correct code to let be helpful for others.
publicclassDialogActivityextendsActivity
{
private Dialog mDialog;
String editTextEnterMobileNum;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder
.setTitle("Incoming Server Message")
.setMessage("text")
.setCancelable(false)
.setPositiveButton("eReceipt?", newDialogInterface.OnClickListener()
{
publicvoidonClick(DialogInterface dialog, int id)
{
dialog.cancel();
mDialog = newDialog(DialogActivity.this);
mDialog.setContentView(R.layout.ereceipt_dialog);
mDialog.setTitle("User Input");
finalEditTextphoneNo= (EditText) mDialog.findViewById(R.id.eReceiptEditText);
mDialog.findViewById(R.id.eReceiptOkButton).setOnClickListener(
newOnClickListener() {
@OverridepublicvoidonClick(View v) {
dismissDialog();
Stringnumber= phoneNo.getText().toString();
try{
android.telephony.SmsManagermSmsManager= android.telephony.SmsManager.getDefault();
mSmsManager.sendTextMessage(number, null, SmsReceiver.msgContent, null, null);
Toast.makeText(getApplicationContext(), "Your SMS has sent successfully!", Toast.LENGTH_LONG).show();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Your SMS sent has failed!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
mDialog.show();
}
})
.setNegativeButton("Exit", newDialogInterface.OnClickListener()
{
publicvoidonClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialogalert= builder.create();
alert.show();
//finish();
}
publicvoiddismissDialog() {
if (mDialog != null) {
mDialog.dismiss();
mDialog = null;
}
}
}
Solution 2:
You are not getting the text from the number field.
Replace the row
mSmsManager.sendTextMessage(editTextEnterMobileNum, null, SmsReceiver.msgContent, null, null);
with this one
mSmsManager.sendTextMessage(editTextEnterMobileNum.getText().toString(), null, SmsReceiver.msgContent, null, null);
Post a Comment for "Why Android.telephony.smsmanager Is Not Able To Send Message From Dialog Activity?"