Question

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();
Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top