Search code examples
androidflashlight

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 when the user presses "Home" key so that user can do other works while torch is on. On back key, the flash turning off is fine. Here is my code. Please help me with it, thanx a lot to all.

public class LightsOn extends Activity implements OnKeyListener
{
    ImageView button;
    Boolean flashon=true;
    private boolean hasFlash;
    Parameters myparas;
    private Camera mycamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light);

        hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

        button=(ImageView)findViewById(R.id.power);
        button.setBackgroundResource(R.drawable.offswitch);
        button.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                if(flashon)
                {
                    Toast.makeText(LightsOn.this, "Flashlight off..!!", Toast.LENGTH_SHORT).show();
                    button.setBackgroundResource(R.drawable.onswitch);
                    if (mycamera == null || myparas == null) 
                    {
                        return;
                    }
                    else
                    {
                        myparas = mycamera.getParameters();
                        myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
                        mycamera.setParameters(myparas);
                        mycamera.stopPreview();
                        flashon=false;
                    }
                }
                else
                {
                    Toast.makeText(LightsOn.this, "Flashlight on..!!", Toast.LENGTH_SHORT).show();
                    button.setBackgroundResource(R.drawable.offswitch);
                    if (mycamera == null || myparas == null) 
                    {
                        return;
                    }
                    else
                    {
                        myparas = mycamera.getParameters();
                        myparas.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        mycamera.setParameters(myparas);
                        mycamera.startPreview();
                        flashon=true;
                    }
                }
            }
        });
    }

    private void getCamera() 
    {
        if (mycamera == null) 
        {
            try 
            {
                mycamera = Camera.open();
                myparas = mycamera.getParameters();
            } 
            catch (RuntimeException e)
            {
                Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
            }
        }

        if (!hasFlash)
        {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("Sorry your device doesnt support flash.");
            dialog.setMessage("Press 'OKAY' to exit..");
            dialog.setPositiveButton("Okay.. :( :(", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) 
                {
                    finish();
                    moveTaskToBack(true);
                }
            });
            dialog.setNegativeButton("More Apps by ****", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) 
                {
                    Toast.makeText(LightsOn.this, "Account Coming Soon..", Toast.LENGTH_SHORT).show();
                    finish();
                    moveTaskToBack(true);
                }
            });
            dialog.setNeutralButton("See website for more", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) 
                {
                    Toast.makeText(LightsOn.this, "Website to be Launched Soon..", Toast.LENGTH_SHORT).show();
                    finish();
                    moveTaskToBack(true);
                }
            });
        dialog.show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();

        // on pause turn off the flash
        myparas = mycamera.getParameters();
        myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
        mycamera.setParameters(myparas);
        mycamera.stopPreview();
        flashon=false;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();    

        if(hasFlash)
        myparas = mycamera.getParameters();
        myparas.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mycamera.setParameters(myparas);
        mycamera.startPreview();
        flashon=true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        // on stop release the camera
        if (mycamera != null)
        {
            mycamera.release();
            mycamera = null;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        // on starting the app get the camera params
        getCamera();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu m)
    {
        MenuInflater inf=getMenuInflater(); 
        inf.inflate(R.menu.menu,m);
        return true;
    }
}

Solution

  • Got solution.

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

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.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.