-1

我制作了一个脚本来检测是否有互联网连接:

    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();
4

2 回答 2

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();
于 2012-09-04T13:52:42.937 回答
0

最简单的方法是创建一个HandlerinonCreate()postDelayed()Runnable调用的 a 一起使用super.onBackPressed()

于 2012-09-06T08:13:10.940 回答