I have an Alert Dialogue Box and I want it to be accessible from a runnable method. The goal is to cancel the dialogue box once it reaches a certain criteria.
Below is the code for creating the AlertDialogue:
public void perm_dialogue()
{
final WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.permission_wifi, null);
perm_prog = (ProgressBar)promptsView.findViewById(R.id.perm_progressBar);
perm_text = (TextView)promptsView.findViewById(R.id.perm_text);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
wifiManager.setWifiEnabled(true);
//call the delay function
handler1.post(DelayTask);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
perm_prog.setVisibility(promptsView.INVISIBLE);
}
Now this is the code I used to make the Runnable:
public Runnable DelayTask = new Runnable()
{
@Override
public void run()
{
final WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//scanning loop for 10 secs
if(wifiManager.isWifiEnabled()==true)
{
//process
perm_prog.setVisibility(View.INVISIBLE);
//Close Alert Dialogue
//stopHandler();
handler1.removeCallbacks(this);
return;
}
else if(k==40)
{
//stopHandler();
handler1.removeCallbacks(this);
return;
}
else
{
perm_prog.setVisibility(View.VISIBLE);
perm_text.setText("Please Wait...");
}
k++;
handler1.postDelayed(DelayTask, 250);
}
};
Now in the runnable, I have placed a Comment tag indicating the place where I want to call the cancel function of the AlertDialogue.
Any Suggession will help! Thanks!
Once try as follows take AlertDialog as member variable
class MyActivity extends Activity{
AlertDialog alertDialog=null;
onCreate(-){
}
}
and create dialog as follows
// create alert dialog
alertDialog = alertDialogBuilder.create();
and then call dismiss() wherever you want in activity as
alertDialog.dismiss();
Hope this will helps you.