ramesh
ramesh

Reputation: 4082

Android application Crashing after quit it by pressing "back button"

As part of my android application development study, I developed a simple SOS application by accessing LED Flash light. Every thing working fine and LED blinking at the interval of 1/2 seconds. But When user press the back button in phone, the application get quit and after 1/2 second a "Crash/Force Close" message coming. I am a noob to android development and whats wrong with my code ?

private boolean lOn=true;



    Timer mTimer = new Timer();
    TimerTask mTimerTask = new TimerTask() {
        @Override
        public void run() {

            if(lOn){
                final Parameters p = camera.getParameters();
                Log.i("info", "torch is turn Off!");
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                camera.startPreview();
                lOn=false;
            }
            else {
                final Parameters p = camera.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();
                isLighOn = false;
                lOn=true;
            }


        }
    };

Upvotes: 3

Views: 677

Answers (2)

Gunaseelan
Gunaseelan

Reputation: 15515

Call purge(); method after calling cancel(); . Why? When calling the cancel method, the timer just stops working. But it didn't close. We have to close it manually before the application closes like calling db.close(); in sqlite. Try this. I think this is only for Timer. I don't know about TimerTask.

mTimer.cancel();
mTimer.purge();

Upvotes: 2

Deepzz
Deepzz

Reputation: 4571

May be because u didn't cancel your timer..

Try this

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if(event.getAction() == KeyEvent.ACTION_DOWN)
        {
            if( keyCode == KeyEvent.KEYCODE_BACK )
            {
                mTimer.cancel();
            }   
        }
        return super.onKeyDown(keyCode, event);
    }

Upvotes: 4

Related Questions