ScheduledExecutorService作为超时显示没有AlertDialog

时间:2013-05-14 10:11:07

标签: java android timeout alertdialog scheduledexecutorservice

我正在尝试在我的应用中实现超时行为。在超时实际发生前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);
}

在onCreate()中调用setTimeout,因此应用程序应在启动后断开5秒。

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后断开连接。

  • 当我将其设置为'timeoutRunnable'时,不会显示alertDialog +即使在“showTimeoutAlertDialog”中的5s之后应该调用'disconnectRunnable',应用也永远无法断开连接!?

我认为ScheduledExecutorService出现问题,但我找不到解决方案。

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

您正在尝试不显示来自UI线程的AlertDialog,因此它永远不会工作从预定线程池中创建的工作线程调用方法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);
    }
}