在android中延迟super.onbackpressed

时间:2012-09-04 13:44:51

标签: android quit

我制作了一个脚本,用于检测是否存在互联网连接:

    public static boolean isOnline() {
    try {
        InetAddress.getByName("google.hu").isReachable(3);
        return true;
    } catch (UnknownHostException e){
        return false;
    } catch (IOException e){
        return false;
    }
}

如果没有互联网,该应用程序将警告用户并退出!但是如何延迟super.onBackPressed 20秒呢? :)

    this.toast1 = new Toast(getApplicationContext());
    toast1.setGravity(Gravity.CENTER, 0, 0);
    toast1.setDuration(20000);
    toast1.setView(layout);
    toast1.show();
    super.onBackPressed();

2 个答案:

答案 0 :(得分:1)

Thread delayThread= new Thread(new Runnable()
                {
                    @Override
                    public void run()
                        {
                            try
                {
                    Thread.sleep(1000);
                }
            catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                            activityInstance.this.runOnUiThread(new Runnable()
                                {
                                    @Override
                                    public void run()
                                        {
                                            super.onBackPressed();
                                        }
                                });
                        }
                });
            delayThread.start();

答案 1 :(得分:0)

最简单的方法是在Handler中创建onCreate(),并使用Runnablesuper.onBackPressed()调用{{1}}