我想在SmsManager中添加一个警告对话框

时间:2014-05-06 09:35:33

标签: android

public void sendTextMessage1(   
            final String destinationAddress, final String scAddress, final String text,
            final PendingIntent sentIntent, final PendingIntent deliveryIntent){  
        if (TextUtils.isEmpty(destinationAddress)) {  
            throw new IllegalArgumentException("Invalid destinationAddress");
        }

        if (TextUtils.isEmpty(text)) {  
            throw new IllegalArgumentException("Invalid message body");  
        }  

        try {  
              final ISms iccISms = ISms.Stub.asInterface(ServiceManager.getService("isms"));
            if (iccISms != null) 
            {
                if( destinationAddress.length()<10 )
                  {

                    new AlertDialog.Builder(null, 0).setTitle("SEND MESSAGE")
                    .setMessage("Are you sure you want to send this msg to no ?   "+ destinationAddress)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) { 
                            // continue with sending

                try
                {

                iccISms.sendText(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
                }
                 catch (RemoteException ex) {
                     // ignore it
                 }
              }
                 })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) { 
                        // do nothing
                    }
                 })
                .setIcon(android.R.drawable.ic_dialog_alert)
                 .show();
              }
              else
              {
                  try
                {
                  iccISms.sendText(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
                }
                 catch (RemoteException ex) {
                       // ignore it
                   }
              }

我正在关注对话框的代码,但它不起作用,任何人都可以建议如何显示此

2 个答案:

答案 0 :(得分:0)

您正在将上下文引用传递为 null 以构建alertDialog,传递上下文引用,否则您将体验 NPE

new AlertDialog.Builder(null, 0)更改为

new AlertDialog.Builder(getApplicationContext(), 0)

答案 1 :(得分:0)

此代码适用于警告对话框

     public class AlertDialogManager {
    /**
     * Function to display simple Alert Dialog
     * @param context - application context
     * @param title - alert dialog title
     * @param message - alert message
     * @param status - success/failure (used to set icon)
     *               - pass null if you don't want icon
     * */
    public void showAlertDialog(Context context, String title, String message,
            Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        if(status != null)
            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }
}

此外,您可以在想要设置警报的地方添加此内容

alert.showAlertDialog(RegisterActivity.this,
                "Internet Connection Error",
                "Please connect to working Internet connection", false);