Skip to content Skip to sidebar Skip to footer

Flashlight App Getting Closed In Background

I am developing a flashlight app and the thing is that the app is working as expected, but not when 'home' or 'back' key is pressed. i want my app to keep the torch light on even w

Solution 1:

Got solution.

What you should do is, write all the code like following in onBackPressed. I merge two methods code in one method.

@OverridepublicvoidonBackPressed() {
    // TODO Auto-generated method stubsuper.onBackPressed();

    myparas = mycamera.getParameters();
    myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
    mycamera.setParameters(myparas);
    mycamera.stopPreview();
    flashon = false;

    if (mycamera != null) {
        mycamera.release();
        mycamera = null;
    }
    Log.d("Camera","Back Pressed");
}

And remove all the code from onStop() and onPause() method. Because when you press Home key, it calls first onPause() and then onStop(). I implement your code and worked fine for me.

Solution 2:

Change you onPause() to this and also onStop() if you need to

@OverrideprotectedvoidonPause() {
    super.onPause();
}

and add this

@OverrideprotectedvoidonBackPressed() { 
    super.onBackPressed();
    // on back turn off the flash
    myparas = mycamera.getParameters();
    myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
    mycamera.setParameters(myparas);
    mycamera.stopPreview();
    flashon=false;
}

Solution 3:

Here I Modified the code set !

It worked for me ! Thanks

@OverrideprotectedvoidonDestroy() {
    super.onDestroy();
}

@OverrideprotectedvoidonPause() {
    super.onPause();

    // on pause turn off the flash//  turnOffFlash();
}

@OverrideprotectedvoidonRestart() {
    super.onRestart();
}

@OverrideprotectedvoidonResume() {
    super.onResume();

    // on resume turn on the flashif(hasFlash)
        turnOnFlash();
}

@OverrideprotectedvoidonStart() {
    super.onStart();

    // on starting the app get the camera paramsgetCamera();
}

@OverridepublicvoidonBackPressed() {
    // TODO Auto-generated method stubsuper.onBackPressed();

    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    isFlashOn = false;

    if (camera != null) {
        camera.release();
        camera = null;
    }
    Log.d("Camera","Back Pressed");
}

@OverrideprotectedvoidonStop() {
    super.onStop();

    // on stop release the camera//   if (camera != null) {//       camera.release();//      camera = null;// }
}

Post a Comment for "Flashlight App Getting Closed In Background"