如何通过编程方式删除android中的alertDialog.builder
我有一个示例代码,显示弹出消息(alertDialog.butiler),每20秒保存在调度程序中。问题是新弹出窗口每20秒就会覆盖旧弹出窗口,所以我如何才能克服这个问题,我需要获得一个弹出窗口,如果弹出窗口处于活动状态,不应该生成启动窗口。所以,请帮助我。如何通过编程方式删除android中的alertDialog.builder
这是我的示例代码:
import android.support.v7.app.AlertDialog; import android.content.DialogInterface;
ScheduledFuture<?> s = null;
private ScheduledExecutorService scheduler = null;
scheduler=Executors.newScheduledThreadPool(1);
s=scheduler.scheduleAtFixedRate(new
Runnable() {
@Override
public void run() {
System.out.println("Called the scheduler");
runOnUiThread(new Runnable() {
@Override
public void run() {
if (showAlertDialog) {
if (!isFinishing()) {
System.out.println("Called the scheduler on ui thread");
AlertDialog.Builder builder = new AlertDialog.Builder(WaitForRiderAllocationActivity.this);
builder.setCancelable(false);
builder.setTitle("TRIP UPDATES");
builder.setMessage("All are our riders are busy, do you want to still continue?");
builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
finish();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(WaitForRiderAllocationActivity.this, "some text", Toast.LENGTH_LONG).show();
Intent intent = new Intent(WaitForRiderAllocationActivity.this, GiveRideTakeRideActivity.class);
startActivity(intent);
finish();
}
});
builder.show();
}
}
}
);
}
},
0, 20, TimeUnit.SECONDS);
回答:
保持AlertBuilder对象全局和您的调度检查中,如果生成器对象为空,并创建您的对话框
// Global object AlertBuilder builder;
...
s = scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("Called the scheduler");
runOnUiThread(new Runnable() {
@Override
public void run() {
if (showAlertDialog){
if(!isFinishing()){
System.out.println("Called the scheduler on ui thread");
if(builder == null){
builder = new AlertDialog.Builder(WaitForRiderAllocationActivity.this); builder.setCancelable(false);
builder.setTitle("TRIP UPDATES");
builder.setMessage("All are our riders are busy, do you want to still continue?");
builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
finish();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(WaitForRiderAllocationActivity.this,"some text",Toast.LENGTH_LONG).show();
Intent intent = new Intent(WaitForRiderAllocationActivity.this, GiveRideTakeRideActivity.class);
startActivity(intent);
finish();
}
});
builder.show();
}
}
}
});
}
},
0, 20,
TimeUnit.SECONDS);
回答:
请参考对话框显示:
if(mDialog!=null) { mDialog.dismiss();
mDialog = null;
}
. . .
mDialog = builder.show();
以上是 如何通过编程方式删除android中的alertDialog.builder 的全部内容, 来源链接: utcz.com/qa/265741.html