ScheduledExecutorService as timeout没有显示AlertDialog



我试图在我的应用程序中实现超时行为。在超时实际发生前5秒还应该有一个警告(alertdialog)。

我想用一个ScheduledExecutorService来做这件事。

这是我到目前为止的相关代码:

private final Context context = this;
private ScheduledExecutorService sExService = Executors.newScheduledThreadPool(2);
private RunnableScheduledFuture<?> sFutureTimeout;
private RunnableScheduledFuture<?> sFutureDisconnect;
private final Runnable timeoutRunnable = new Runnable(){
    @Override
    public void run() {     
        showTimeoutAlertDialog();
    }   
};
private final Runnable disconnectRunnable = new Runnable(){
    @Override
    public void run() {
        disconnect();   
    }   
};

和处理超时行为的方法:

private void setTimeout(){
    sFutureTimeout = (RunnableScheduledFuture<?>) sExService.schedule(timeoutRunnable, 5, TimeUnit.SECONDS);
}

setTimeout在onCreate()中被调用,所以应用程序应该在启动后5s断开连接。

private void showTimeoutAlertDialog(){
    new AlertDialog.Builder(context)
            .setTitle("Disconnect in 5s")
            .setCancelable(false)
            .setPositiveButton("Abort",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            sFutureDisconnect.cancel(false);
                            setTimeout();
                        }
                    }).show();  
    sFutureDisconnect = (RunnableScheduledFuture<?>) sExService.schedule(disconnectRunnable, 5, TimeUnit.SECONDS);
}

这是我面临的问题:

  • 如果在"setTimeout"中调用的runnable被设置为'disconnectRunnable',它工作正常,应用程序在5s后断开连接。

  • 当我将其设置为' timeoutunnable '时,alertDialog不显示+应用程序永远不会断开连接,即使'disconnectRunnable'应该在5s后在"showtimeoutertdialog"中调用!

我认为ScheduledExecutorService在这里出了问题,但我找不到解决方案。

谢谢你的帮助

您试图显示AlertDialog不是来自UI线程,所以它永远不会工作。方法showTimeoutAlertDialog()是从计划线程池中创建的工作线程调用的。您可以使用Handler来实现您的目的:

public class MyActivity extends Activity {
    private final Context context = this;
    private static Handler mHandler = new Handler();
    private final Runnable timeoutRunnable = new Runnable(){
        @Override
        public void run() {
            showTimeoutAlertDialog();
        }
    };
    private final Runnable disconnectRunnable = new Runnable(){
        @Override
        public void run() {
            disconnect();
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTimeout();
    }
    private void disconnect() {
        Log.e("MyActivity", "Disconnected");
    }
    private void setTimeout(){
        mHandler.postDelayed(timeoutRunnable, 5000);
    }
    private void showTimeoutAlertDialog(){
        new AlertDialog.Builder(context)
                .setTitle("Disconnect in 5s")
                .setCancelable(false)
                .setPositiveButton("Abort",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                mHandler.removeCallbacks(disconnectRunnable);
                                setTimeout();
                            }
                        }).show();
        mHandler.postDelayed(disconnectRunnable, 5000);
    }
}

相关内容

  • 没有找到相关文章

最新更新