android:警报对话框不等待输入
我有一个 Android 应用程序,可以扫描 SD 卡并播放位于 SD 卡上的文件。但是,我想先检查 SD 卡是否在插槽中,然后才能继续处理。但是,警报对话框不会等待输入并继续处理后台,这会出现错误,因为没有 SD 卡并且后台处理尝试访问该 SD 卡。我想等到完成警报对话框。有什么办法可以做到这一点吗?这是警报对话框的代码。我只想让程序等待,直到用户选择该选项。
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在用户按下对话框的按钮之一之前不要执行任何操作。由于如果未安装 SD 卡,您无能为力,因此只需单击“请安装 SD 卡以继续”和“确定”按钮即可
finish()
-es 活动。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.我很早就尽力回答了你的问题,但我不明白你为什么要加倍,因为我觉得我的答案已经足够清楚了。您的 SD 卡检查必须以阻塞方式调用,但您的应用程序在获得有关 SD 卡存在的明确信息之前不会继续进行。似乎您正在尝试在单独的线程中执行某些操作,同时在 UI 线程中检查 SD 卡 - 这是完全错误的!检查SD卡,确保它存在,然后做任何你想做的事情。我希望我现在已经清楚了,如果您仍然遗漏任何内容,请务必提出您的问题。此致。
I've tried my best to answer your question early on, and I can't understand why you've doubled it, since I feel my answer was clear enough. Your SD card checking must be called in a blocking manner, though your application will not proceed until it gets the clear information about the presence of the SD card. Seems like you're trying to do something in a separate thread while checking the SD card in the UI thread - this is completely wrong! Check the SD card, make sure that it exists, and then do whatever you want to do. I hope I was clear now, if you're still missing anything - be sure to ask your questions. Best regards.
您可以尝试检查您的外部存储文件夹是否存在作为内存检查的块。
You can try to check if your external storage folder exist or not as a block for memory check.