我是 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;
}
}*/
}