I have an android application that scans the sd-card and plays file that are located on the sd-card. however, i want to first check whether sd-card is in the slot or not before i can continue processing. however, the alert dialog doesn’t wait for the input and continue processing the background which gives errors because there is no sd-card and the background processing tries to access that sd-card. I want to wait until I am finished with the alert dialog. is there any way of doing this?? Here is the code for the alert dialog. I just want the program to wait until the user selects the option..
String state = Environment.getExternalStorageState();
if (state.equalsIgnoreCase("mounted"))
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("The SD Card has been removed from your device. Please re-insert the SD card and try again")
.setCancelable(false)
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id)
{
onRecreate();
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
// onDestroy();
finish();
}
})
.setTitle("No SD-card Found")
.setIcon(R.drawable.alert_dialog_icon_m);
AlertDialog alert = builder.create();
alert.show();
return 0;
}//IF condition closes
else
return 1;
Simply don’t do anything until the user presses one of the dialog’s buttons. Since there is not much you can do if the SD card is not mounted, just have a ‘Please mount SD card to continue’ and an ‘OK’ button that
finish()-es the activity.