Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

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 mysteriously the dialog is not able to send sms to the recipient. Would you please help me to understand the reason ?

Here is the code :

package com.example.demo;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DialogActivity extends Activity 
{   
    private Dialog mDialog;
    String editTextEnterMobileNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder
        .setTitle("Incoming Server Message")
        .setMessage("text")
        .setCancelable(false)
        .setPositiveButton("eReceipt?", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                dialog.cancel();

                mDialog = new Dialog(DialogActivity.this);
                mDialog.setContentView(R.layout.ereceipt_dialog);                       
                mDialog.setTitle("User Input");
                editTextEnterMobileNum = ((EditText) mDialog.findViewById(R.id.eReceiptEditText)).getText().toString();
                mDialog.findViewById(R.id.eReceiptOkButton).setOnClickListener(
                        new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                dismissDialog();
                                try{
                                    android.telephony.SmsManager mSmsManager = android.telephony.SmsManager.getDefault();
                    mSmsManager.sendTextMessage(editTextEnterMobileNum, 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", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                dialog.cancel();

            }
        });
        AlertDialog alert = builder.create();
        alert.show();
        //finish();
    }

    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

}

And The Logcat output is :

03-13 18:09:58.139: E/ViewRootImpl(21086): sendUserActionEvent() mView == null
03-13 18:10:13.999: W/System.err(21086): java.lang.IllegalArgumentException: Invalid destinationAddress
03-13 18:10:14.009: W/System.err(21086):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:120)
03-13 18:10:14.009: W/System.err(21086):    at com.watchdata.qposdemo.DialogActivity$1$1.onClick(DialogActivity.java:66)
03-13 18:10:14.009: W/System.err(21086):    at android.view.View.performClick(View.java:4475)
03-13 18:10:14.009: W/System.err(21086):    at android.view.View$PerformClick.run(View.java:18786)
03-13 18:10:14.009: W/System.err(21086):    at android.os.Handler.handleCallback(Handler.java:730)
03-13 18:10:14.009: W/System.err(21086):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-13 18:10:14.009: W/System.err(21086):    at android.os.Looper.loop(Looper.java:176)
03-13 18:10:14.009: W/System.err(21086):    at android.app.ActivityThread.main(ActivityThread.java:5419)
03-13 18:10:14.009: W/System.err(21086):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 18:10:14.009: W/System.err(21086):    at java.lang.reflect.Method.invoke(Method.java:525)
03-13 18:10:14.009: W/System.err(21086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
03-13 18:10:14.009: W/System.err(21086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
03-13 18:10:14.009: W/System.err(21086):    at dalvik.system.NativeStart.main(Native Method)
03-13 18:10:14.629: E/ViewRootImpl(21086): sendUserActionEvent() mView == null
03-13 18:10:14.724: D/AbsListView(21086): onVisibilityChanged() is called, visibility : 8
03-13 18:10:14.724: D/AbsListView(21086): unregisterIRListener() is called 
03-13 18:10:14.729: D/AbsListView(21086): Get MotionRecognitionManager
03-13 18:10:14.744: D/AbsListView(21086): onVisibilityChanged() is called, visibility : 4
03-13 18:10:14.744: D/AbsListView(21086): unregisterIRListener() is called 
03-13 18:10:14.749: D/AbsListView(21086): onVisibilityChanged() is called, visibility : 0
03-13 18:10:14.749: D/AbsListView(21086): unregisterIRListener() is called 
03-13 18:10:14.854: D/AbsListView(21086): unregisterIRListener() is called 

During debugging, execution fails to enter into OnClickListener() of the

mDialog.findViewById(R.id.eReceiptOkButton).setOnClickListener(
                        new OnClickListener() {

I am not able to figure out why ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

public class DialogActivity extends Activity 
{   
    private Dialog mDialog;
    String editTextEnterMobileNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder
        .setTitle("Incoming Server Message")
        .setMessage("text")
        .setCancelable(false)
        .setPositiveButton("eReceipt?", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                dialog.cancel();

                mDialog = new Dialog(DialogActivity.this);
                mDialog.setContentView(R.layout.ereceipt_dialog);                       
                mDialog.setTitle("User Input");
                final EditText phoneNo = (EditText) mDialog.findViewById(R.id.eReceiptEditText);
                mDialog.findViewById(R.id.eReceiptOkButton).setOnClickListener(
                        new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                dismissDialog();
                                String number = phoneNo.getText().toString();
                                try{
                                    android.telephony.SmsManager mSmsManager = 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", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int id) 
            {
                dialog.cancel();

            }
        });
        AlertDialog alert = builder.create();
        alert.show();
        //finish();
    }

    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...