-1

I made a script which detects if there's internet connection:

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

If there's no internet the app will warn the user and the quit! But how to delay super.onBackPressed for 20 seconds? :)

    this.toast1 = new Toast(getApplicationContext());
    toast1.setGravity(Gravity.CENTER, 0, 0);
    toast1.setDuration(20000);
    toast1.setView(layout);
    toast1.show();
    super.onBackPressed();
Simulator88
  • 617
  • 6
  • 12
  • 27

2 Answers2

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();
Shachillies
  • 1,616
  • 14
  • 12
  • `d.start()` should be `delayThread.start()`. Of course Android 4.0 supports `Thread`, why do you think it doesn't? Also, this thread sleeps for 1 second not 20, so you should use `Thread.sleep(20000)`. And you don't want to call `super.onBackPressed()`, you should just call `finish()` on the activity. – David Wasser Sep 04 '12 at 14:55
  • Guys d.start() was a typo mistake . please forgive me for that (wink) – Shachillies Sep 06 '12 at 07:24
0

The easiest is to create a Handler in onCreate()and use postDelayed() with a Runnable that calls super.onBackPressed()

Aert
  • 1,989
  • 2
  • 15
  • 17