Search code examples
androidmultithreadingonresume

problem with starting a thread in Android


I have an activity in Android which displays as a background a picture.When I click on the background I use an intent to go at another activity.... Here is how my code looks like:

public class Precisari extends Activity{

    protected boolean _active = true;
    protected int _splashTime = 10000;
    public SplashThread  splashThread;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.precisari);

     splashThread=new SplashThread();

    }


    public class SplashThread extends Thread
    {
        public SplashThread()
        {

        }


        public void run() {

            try {

                int waited = 0;
                while (_active && (waited < _splashTime)) {
                    sleep(100);
                    if (_active) {

                        waited += 100;
                    }
                }
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                //finish();
                Intent i = new Intent(getBaseContext(), com.SplashScreen.ConnectWithFb.class);
                startActivity(i);
                stop();

            }
        }

    }



    public boolean onTouchEvent(MotionEvent event){
        if(event.getAction()==MotionEvent.ACTION_DOWN){

            _active=false;
            }
        return true;
    }


    public void onResume(){
        super.onResume();
        _active=true;
        splashThread.start();
    }

    public void onPause()
    {
        super.onPause();
        splashThread.stop();

    }


}

I'm using a thread which I started in onResume()...this thread loops until boolean variabile _active is set to false .The variable is set to false when I click on the screen of the emulator:

public boolean onTouchEvent(MotionEvent event){
            if(event.getAction()==MotionEvent.ACTION_DOWN){

                _active=false;
                }
            return true;
        }

and then I pass to the next activity by using an intent:

Intent i = new Intent(getBaseContext(), com.SplashScreen.ConnectWithFb.class);

The thread is started in onResume()and stopped in onPause().The idea is that when I come back to this activity I want the same idea to work....to pass to the next activity when I click the screen!

Here is how my logcat looks like:

 Uncaught handler: thread main exiting due to uncaught exception
 java.lang.RuntimeException: Unable to resume activity {com.SplashScreen/com.SplashScreen.Precisari}: java.lang.IllegalThreadStateException: Thread already started.
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2950)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2965)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1889)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4363)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.IllegalThreadStateException: Thread already started.               at java.lang.Thread.start(Thread.java:1322)
    at com.SplashScreen.Precisari.onResume(Precisari.java:101)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
    at android.app.Activity.performResume(Activity.java:3763)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2937)

Could please someone tell me what I'm doing wrong??


Solution

  • put sleep(100) in try catch block.