我是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; } }*/ }
dialog.setCancelable(true); should be like dialog.setCancelable(false);
当用户按下返回按钮时,它可以防止关闭警报对话框:)