Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deprecated thread methods are not supported

Tags:

android

I am making one project where i need to display Home page and when home page displays, after that or continue with that 3 to 5 seconds my other welcome custom dialog is display. but making that, following error occurs but my application doesn't stop working.. LogCat displays these errors. Application Code:

  final Dialog d=new Dialog(Main.this);
    d.setContentView(R.layout.SplashScreen);
    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                d.show();

                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }
            } catch(InterruptedException e) {
                // do nothing
            } finally {
                d.cancel();
                    stop();
            }
        }
    };
    splashTread.start();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        _active = false;
    }
    return true;
}

The error in LogCat:

12-30 14:54:54.044: E/global(1232): Deprecated Thread methods are not supported.
12-30 14:54:54.044: E/global(1232): java.lang.UnsupportedOperationException.
12-30 14:54:54.044: E/global(1232):     at java.lang.VMThread.stop(VMThread.java:85)
12-30 14:54:54.044: E/global(1232):     at java.lang.Thread.stop(Thread.java:1280)
12-30 14:54:54.044: E/global(1232):     at java.lang.Thread.stop(Thread.java:1247)
12-30 14:54:54.044: E/global(1232):     at com.droidnova.android.SplashScreen$1.run(SplashScreen.java:35)
like image 699
Dharmik Avatar asked Dec 01 '25 08:12

Dharmik


1 Answers

In Android its better to use Handler for managing the Thread and Runnables

Create an Handler instance

Handler handler = new Handler();

Create a Runnable thread

Runnable runnable = new Runnable() {

        @Override
        public void run() {
            Log.d("runnable started", "inside run");
            handler.removeCallbacks(runnable);
            handler.postDelayed(runnable, 1000);
        }
    };

And start the Runnable using Handler

handler.postDelayed(runnable, 1000);

And to stop the Runnable use

handler.removeCallbacks(runnable);
like image 98
Lalit Poptani Avatar answered Dec 03 '25 23:12

Lalit Poptani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!