Search code examples
javaandroidandroid-activityjava-native-interfaceandroid-service

Android Alert Message Need Mandatory Input


I have developed an Android Service which will run in the background. The Service want to accept Yes or No confirmation from the user at an event(say when receive an SMS). Its working fine; the Yes or No question will be shown to user. But i want the input from (press YES or NO) user without exit from the alert(exit by press on the Mobile Back button or Home button etc). Please help me how it can be possible. Below the code I am using;

 AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
//ctx is the Context
            builder.setTitle("Emergency!"); 
            String txt="Do you want to accept?"; 
            builder.setMessage(txt);
            builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //Do something 
                }
                });
                builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ///dialog.dismiss();
                    }
                });

                AlertDialog alert = builder.create();
                alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                alert.show(); 

Solution

  • just add this in you AlertDialog

     builder.setCancelable(false);
    

    hope you are useing android.support.v7.app.AlertDialog;