2012-05-14 115 views
2

我是Android新手,我已經完全綁定了我的應用程序,但我希望現在可以對其進行更改。在發送短信之前顯示確認對話框

當有人發送短信,我想顯示一個確認對話框發送短信要求發送與否。我必須爲我寫的源代碼編寫一個通用的類,它的工作正常,但如果用戶沒有點擊是或取消,對話框會熄滅,但我希望它仍然會顯示,直到用戶不點擊是或取消按鈕。

如何解決?

public void send_SMS(final String message) 
    { 
    try{ 


     AlertDialog.Builder dialog = new AlertDialog.Builder(ctx); 
     dialog.setTitle("Send SMS"); 
     dialog.setMessage("Are U sure want to Send SMS"); 
     dialog.setCancelable(true); 
     dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       sentpi = PendingIntent.getBroadcast(ctx, 0, new Intent(SENT),0); 
       delpi = PendingIntent.getBroadcast(ctx, 0, new Intent(DELIVERED), 0);// 
       String id = message.substring(3, 4); 
       int msgId = Integer.valueOf(id, 16).intValue(); 

       if(dba.IsConOpenOrClose()==false) 
        dba.Open(); 
       if(ConstantClass.SelectCord.equals("All")){ 
        if(msgId == 3 || msgId == 9){ 
        devId = message.substring(4, 20); 
        ConstantClass.CurrentId = devId; 
        strSql = new String("SELECT * FROM "+ ConstantClass.dbName[2] + " WHERE " + DataBaseAdapter.Key_DevID + " = ?"); 
        cur = dba.ExecuteSelect(strSql, devId); 
        if(cur!=null) 
         cur.moveToFirst(); 
        int cordId = cur.getInt(cur.getColumnIndex(DataBaseAdapter.Key_DevCoordId)); 
        phoneNumber = dba.getSendSmsMobileNo(cordId); 
        dba.prcUpdateOrClean(devId, 1); 
        cur.close(); 
        } 
       }else{ 
        phoneNumber = dba.getSendSmsMobileNo(dba.CurrentWorkingCoordinator(DataBaseAdapter.Key_SendCoord, 1)); 
       /*********now getting the Field smsProcess Update for particular devices ********************/ 
       if(msgId == 3 || msgId == 9 || msgId == 7){ 
        devId = message.substring(4, 20); 
        if(dba.IsConOpenOrClose()==false) 
         dba.Open(); 
        ConstantClass.CurrentId = devId; 
        dba.prcUpdateOrClean(devId, 1); 

       }else if(msgId == 15 || msgId == 13 || msgId == 11 || msgId == 1){ 
        if (dba.IsConOpenOrClose()==false) 
         dba.Open(); 
        phoneNumber = dba.getSendSmsMobileNo(dba.CurrentWorkingCoordinator(DataBaseAdapter.Key_SendCoord, 1)); 
        if(phoneNumber.length()==11){ 
         pdNo = phoneNumber.substring(1, 11); 
        }else{ 
         pdNo = phoneNumber.substring(0, 10); 
        } 
        devId = "000000" + pdNo; 
        ConstantClass.CurrentId = devId;     
        dba.prcUpdateOrClean(devId, 1); 

       } 
       } 
        SmsManager sms = SmsManager.getDefault(); 
        if(phoneNumber!=null){ 
         sms.sendTextMessage(phoneNumber, null, message.toUpperCase(), sentpi, delpi); 
        }else{ 
         throw new java.lang.NullPointerException() ; 
        }    
      } 
     }); 
     dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       dialog.cancel(); 
      } 
     }); 
     AlertDialog dlg = dialog.create(); 
     dlg.show();  
    }catch(Throwable e){ 
     dlg = new ExceptionDialog(this.ctx,"Sending SMS",e.getMessage()); 
     dlg.show(); 
    } 
} 
public BroadcastReceiver sms_send = new BroadcastReceiver(){ 
    @Override 
    public void onReceive(Context context,Intent intent){ 
     String Info = "Send Info:"; 
     switch(getResultCode()){ 
     case Activity.RESULT_OK: 
      Info += "Sms Send Successfull"; 
      break; 
     case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
      Info += "Sms Sending faild : genric faliure"; 
      break; 
     case SmsManager.RESULT_ERROR_NO_SERVICE: 
      Info += "Sending failed due to 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 Service OFF"; 
      break; 
     } 
     Toast.makeText(ctx, Info, Toast.LENGTH_SHORT).show(); 
    } 
}; 
public BroadcastReceiver sms_delivered = new BroadcastReceiver(){ 
    @Override 
    public void onReceive(Context context, Intent intent){ 
     String Info = "SMS Delivered :"; 
     switch(getResultCode()){ 
     case Activity.RESULT_OK: 
      Info += "Sms Deliverd"; 
      break; 
     case Activity.RESULT_CANCELED: 
      Info += "Sms not Delivered"; 
      break; 
     } 
     Toast.makeText(ctx, Info, Toast.LENGTH_SHORT).show(); 
    } 
}; 
public IntentFilter getSentIntentFilter(){ 
    return new IntentFilter(SENT);  
} 
public IntentFilter getDeliveredFilter(){ 
    return new IntentFilter(DELIVERED); 
} 
/*private void reThrow(MyException ex){ 
    if(ex.nullPointerException instanceof NullPointerException ){ 

    } 
} 
private class MyException extends Exception{ 
    public Exception nullPointerException; 
    public MyException(){} 
    public MyException(Exception NullPointerException){ 
     this.nullPointerException = NullPointerException;  
    } 
}*/ 
} 

回答

2
dialog.setCancelable(true); should be like dialog.setCancelable(false); 

它可以防止警告對話框關閉,當用戶按下後退按鈕:)

2

用我的類:

import java.util.ArrayList; 

import org.mabna.order.R; 
import org.mabna.order.dataModel.EntMapMovementsCollection; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.graphics.Typeface; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MessageBox { 

    static ArrayList<AlertDialog> arrAlertDialog = new ArrayList<AlertDialog>(); 
    static OnMyDialogResult mDialogResult; // the callback 

    public enum MessageBoxState { 
     OK, YesNo 
    } 

    public enum MessageBoxResult { 
     OK, Cancel, Yes, No 
    } 

    public final static void show(final Activity parent, String title, 
      String message, MessageBoxState state) { 

     // if (alertDialog != null) 
     // { 
     // alertDialog.dismiss(); 
     // } 

     AlertDialog.Builder builder; 

     LayoutInflater inflater = (LayoutInflater) parent 
       .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.dialog_messagebox, 
       (ViewGroup) parent.findViewById(R.id.layout_root)); 
     layout.setMinimumWidth(400); 

     Typeface tf = Farsi.GetFarsiFont(parent); 

     TextView tvTitle = (TextView) layout.findViewById(R.id.tvTitle); 
     tvTitle.setText(Farsi.Convert(title)); 
     tvTitle.setTypeface(tf); 
     tvTitle.setPadding(10, 10, 10, 10); 

     TextView tvMessage = (TextView) layout.findViewById(R.id.tvMessage); 
     tvMessage.setText(Farsi.Convert(message)); 
     tvMessage.setTypeface(tf); 
     tvMessage.setPadding(10, 10, 10, 10); 

     Button btnOK = (Button) layout.findViewById(R.id.btnOK); 
     Button btnYes = (Button) layout.findViewById(R.id.btnYes); 
     Button btnNo = (Button) layout.findViewById(R.id.btnNo); 
     Button btnCancel = (Button) layout.findViewById(R.id.btnCancel); 
     btnOK.setText(Farsi.Convert("تایید")); 
     btnOK.setTypeface(tf); 
     LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) btnOK 
       .getLayoutParams(); 
     lp.setMargins(10, 10, 10, 10); 
     btnOK.setLayoutParams(lp); 

     btnYes.setText(Farsi.Convert("بله")); 
     btnYes.setTypeface(tf); 
     lp = (LinearLayout.LayoutParams) btnYes 
       .getLayoutParams(); 
     lp.setMargins(10, 10, 10, 10); 
     btnYes.setLayoutParams(lp); 

     btnNo.setText(Farsi.Convert("خیر")); 
     btnNo.setTypeface(tf); 
     btnNo.setPadding(10, 10, 10, 10); 
     lp = (LinearLayout.LayoutParams) btnNo 
       .getLayoutParams(); 
     lp.setMargins(10, 10, 10, 10); 
     btnNo.setLayoutParams(lp); 

     btnCancel.setText(Farsi.Convert("انصراف")); 
     btnCancel.setTypeface(tf); 
     btnCancel.setPadding(10, 10, 10, 10); 
     lp = (LinearLayout.LayoutParams) btnCancel 
       .getLayoutParams(); 
     lp.setMargins(10, 10, 10, 10); 
     btnCancel.setLayoutParams(lp); 

     btnOK.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       btnOK_onClick(); 
      } 
     }); 
     btnYes.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       btnYes_onClick(); 
      } 
     }); 
     btnNo.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       btnNo_onClick(); 
      } 
     }); 
     btnCancel.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       btnCancel_onClick(); 
      } 
     }); 

     LinearLayout layout_OK = (LinearLayout) layout 
       .findViewById(R.id.layout_OK); 
     LinearLayout layout_YesNo = (LinearLayout) layout 
       .findViewById(R.id.layout_YesNo); 

     switch (state) { 
     case OK: 
      layout_YesNo.setVisibility(LinearLayout.GONE); 
      layout_OK.setVisibility(LinearLayout.VISIBLE); 
      break; 
     case YesNo: 
      layout_YesNo.setVisibility(LinearLayout.VISIBLE); 
      layout_OK.setVisibility(LinearLayout.GONE); 
      break; 
     default: 
      break; 
     } 

     builder = new AlertDialog.Builder(parent); 
     builder.setView(layout); 

     AlertDialog alertDialog; 

     alertDialog = builder.create(); 
     alertDialog.setCancelable(false); 
     alertDialog.show(); 
     mDialogResult = null; // causes each messageBox instance has its own 
           // DialogResult because it removes previous 
           // DialogResults 

     arrAlertDialog.add(alertDialog); 
     return; 
    } 

    public static void showException(Activity act, 
      Exception e) { 
     MessageBox.show(act, "خطا", e.toString(), MessageBoxState.OK); 
    } 

    public static void showException(Activity act, String additionalDetails, 
      Exception e) { 
     MessageBox.show(act, "خطا", additionalDetails + ": " + e.toString(), 
       MessageBoxState.OK); 
    } 

    private static AlertDialog getLastDialog() { 
     AlertDialog dialog = arrAlertDialog.get(arrAlertDialog.size() - 1); 
     arrAlertDialog.remove(dialog); 
     return dialog; 
    } 

    private final static void btnOK_onClick() { 
     AlertDialog currentdialog = getLastDialog(); 
     if (mDialogResult != null) { 
      mDialogResult.finish(MessageBoxResult.OK); 
      currentdialog.dismiss(); 
      mDialogResult.dialogClosed(); 
     } else { 
      currentdialog.dismiss(); 
     } 
    } 

    private final static void btnYes_onClick() { 
     AlertDialog currentdialog = getLastDialog(); 
     if (mDialogResult != null) { 
      mDialogResult.finish(MessageBoxResult.Yes); 
      currentdialog.dismiss(); 
      mDialogResult.dialogClosed(); 
     } else { 
      currentdialog.dismiss(); 
     } 
    } 

    private final static void btnNo_onClick() { 
     AlertDialog currentdialog = getLastDialog(); 
     if (mDialogResult != null) { 
      mDialogResult.finish(MessageBoxResult.No); 
      currentdialog.dismiss(); 
      mDialogResult.dialogClosed(); 
     } else { 
      currentdialog.dismiss(); 
     } 
    } 

    private final static void btnCancel_onClick() { 
     AlertDialog currentdialog = getLastDialog(); 
     if (mDialogResult != null) { 
      mDialogResult.finish(MessageBoxResult.Cancel); 
      currentdialog.dismiss(); 
      mDialogResult.dialogClosed(); 
     } else { 
      currentdialog.dismiss(); 
     } 
    } 

    public final static void setDialogResult(OnMyDialogResult dialogResult) { 
     mDialogResult = dialogResult; 
    } 

    public interface OnMyDialogResult { 
     void finish(MessageBoxResult result); 

     void dialogClosed(); 
    } 

} 

其佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout_root" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white" 
    android:orientation="vertical" 
    android:padding="10dp" > 

    <TextView 
     android:id="@+id/tvTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     android:text="TextView" 
     android:textStyle="bold" > 
    </TextView> 

    <TextView 
     android:id="@+id/tvLine" 
     android:layout_width="fill_parent" 
     android:layout_height="2dip" 
     android:layout_weight="1" 
     android:background="@color/black" 
     android:text="" > 
    </TextView> 

    <TextView 
     android:id="@+id/tvMessage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     android:text="TextView" > 
    </TextView> 

    <LinearLayout 
     android:id="@+id/layout_YesNo" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center" > 

     <Button 
      android:id="@+id/btnCancel" 
      android:layout_width="70dip" 
      android:layout_height="40dip" 
      android:text="Button" > 
     </Button> 

     <Button 
      android:id="@+id/btnNo" 
      android:layout_width="70dip" 
      android:layout_height="40dip" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/btnYes" 
      android:layout_width="70dip" 
      android:layout_height="40dip" 
      android:text="Button" > 
     </Button> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/layout_OK" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:baselineAligned="true" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/btnOK" 
      android:layout_width="80dip" 
      android:layout_height="40dip" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:text="Button" > 
     </Button> 
    </LinearLayout> 

</LinearLayout> 

和活動:

MessageBox.show(this, "warning", "Are you sure?", MessageBoxState.YesNo); 
MessageBox.setDialogResult(new OnMyDialogResult() { 
        @Override 
        public void finish(MessageBoxResult result) { 
         if (result == MessageBoxResult.Yes) 
          //... 
        } 

        @Override 
        public void dialogClosed() { 
        } 
       }); 
+0

非常感謝 –