Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ScheduledExecutorService作为超时显示无警报对话框_Java_Android_Timeout_Android Alertdialog_Scheduledexecutorservice - Fatal编程技术网

Java ScheduledExecutorService作为超时显示无警报对话框

Java ScheduledExecutorService作为超时显示无警报对话框,java,android,timeout,android-alertdialog,scheduledexecutorservice,Java,Android,Timeout,Android Alertdialog,Scheduledexecutorservice,我正在尝试在我的应用程序中实现超时行为。在超时实际发生前5秒,还应该有一个警告(alertdialog) 我想使用ScheduledExecutorService来执行此操作 以下是我目前的相关代码: private final Context context = this; private ScheduledExecutorService sExService = Executors.newScheduledThreadPool(2); private RunnableScheduledF

我正在尝试在我的应用程序中实现超时行为。在超时实际发生前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 final Context=this;
private ScheduledExecutorService sExService=Executors.newScheduledThreadPool(2);
私有RunnableScheduledFuture sFutureTimeout;
私有可运行的可调度未来sFutureDisconnect;
private final Runnable timeoutRunnable=new Runnable(){
@凌驾
public void run(){
showTimeoutAlertDialog();
}   
};
private final Runnable disconnectRunnable=new Runnable(){
@凌驾
公开募捐{
断开连接();
}   
};
以及处理超时行为的方法:

private void setTimeout(){
    sFutureTimeout = (RunnableScheduledFuture<?>) sExService.schedule(timeoutRunnable, 5, TimeUnit.SECONDS);
}
private void setTimeout(){
sFutureTimeout=(RunnableScheduledFuture)sExService.schedule(timeoutRunnable,5,TimeUnit.SECONDS);
}
setTimeout在onCreate()中调用,因此应用程序应在启动后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);
}
private void showTimeoutAlertDialog(){
新建AlertDialog.Builder(上下文)
.setTitle(“断开5s连接”)
.setCancelable(错误)
.setPositiveButton(“中止”,
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface对话框,int-id){
sFutureDisconnect.cancel(假);
setTimeout();
}
}).show();
sFutureDisconnect=(RunnableScheduledFuture)sExService.schedule(disconnectRunnable,5,TimeUnit.SECONDS);
}
以下是我面临的问题:

  • 如果在“setTimeout”中调用的runnable设置为“disconnectRunnable”,则运行正常,应用程序在5s后断开连接

  • 当我将其设置为“timeoutRunnable”时,不会显示alertDialog+即使在“showTimeoutAlertDialog”中5秒后应调用“disconnectRunnable”,应用程序也不会断开连接

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


感谢您的帮助:)

您试图显示AlertDialog而不是从UI线程,因此它将永远无法工作。方法
showTimeoutAlertDialog()
是从计划线程池中创建的工作线程调用的。您可以使用
处理程序
实现以下目的:

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);
    }
}

非常感谢。我以前试过使用处理器。我不知道。removeCallbacks(r)方法:)还有一个问题:现在线程怎么样?整个超时行为现在都在ui线程上运行,不是吗?如果是,是否有将超时功能保留在单独线程中的解决方法?如果此处将执行长时间运行的作业,则可以在
disconnect()
方法中启动新线程
mHandler.postDelayed(…)
不会阻止UI线程,它只会将Runnable放入消息队列,并将其延迟指定的时间。如果需要,还可以从此Runnable启动新线程。