1

Create a simple Torch application which can put on the Camera Flash and put it off. This application should have a simple UI which can put On the Flash and also put it Off. After 1 minute of continuous use, the UI should prompt the user he wants to keep use the Torch. If the user says Yes the Torch shall remain on for another minute and this cycle shall repeat. If the user says No, the Torch shall be put off.

Creating a simple torch app is easy and I have made torch app but the second thing which is mentioned in the question to make a prompt after 1 minute that thing I am not able to understand can anyone help?

here is my code of flashlight can anyone tell me where to change in it.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends AppCompatActivity
{
    `enter code here`private boolean isLightOn=false;
    private Camera camera;
    private Button btn;


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

       if(camera!= null)
           camera.release();
   }



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button);
        Context context=this;

        PackageManager pk= context.getPackageManager();

        if(!pk.hasSystemFeature(PackageManager.FEATURE_CAMERA))
        {
            Log.e("err","Device has no camera");
            return;
        }
        camera=camera.open();
        final Parameters p= camera.getParameters();
        btn.setOnClickListener(new OnClickListener()
        {
         @Override
        public void onClick(View v)
         {

             if(isLightOn)
             {
                 Log.i("info","FlashLight is turn off");
                 p.setFlashMode(Parameters.FLASH_MODE_OFF);
                 camera.setParameters(p);
                 camera.stopPreview();
                 isLightOn=false;

             }
             else
             {
                 Log.i("info","FlashLight is turn On!");
                 p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                 camera.setParameters(p);
                 camera.startPreview();
                 isLightOn=true;

             }
         }

        });

    }
}
Vikas Kumar
  • 143
  • 10

4 Answers4

1
  1. First create a XML having a switch widget. Then connect it with Java, Using onOffSwitch = (Switch) findViewById(R.id.switch1);
  2. Get CameraManager so that u can manage camera's element(which is the flashLight), Using mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 3.Get current phone's camera ID so that you can instruct android which camera to Point , Using mCameraId = mCameraManager.getCameraIdList()[0];
  3. Turn on using : mCameraManager.setTorchMode(mCameraId, true);
  4. Turn off Using : mCameraManager.setTorchMode(mCameraId, false);

Code:

public class TorchActivity extends AppCompatActivity {

    CameraManager mCameraManager;
    String mCameraId;
    Switch onOffSwitch;

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

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        WindowManager.LayoutParams layout = getWindow().getAttributes();
        layout.screenBrightness = 1F;
        getWindow().setAttributes(layout);


        onOffSwitch = (Switch) findViewById(R.id.switch1);

        mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            mCameraId = mCameraManager.getCameraIdList()[0];

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        Boolean isFlashAvailable = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        if (isFlashAvailable)
        {
            onOffSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onOffSwitch.isChecked()) {

                        try {
                            mCameraManager.setTorchMode(mCameraId, true);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    else{

                        try {
                            mCameraManager.setTorchMode(mCameraId, false);
                        }
                        catch (CameraAccessException e) {
                            e.printStackTrace();

                        }
                    }
                }
            });

        }else
        {
            Toast.makeText(this, "No Flash Support Found!", Toast.LENGTH_SHORT).show();
        }
}

//Use these permissions!

 Min SDK should be -  23
//<uses-permission android:name="android.permission.CAMERA" />
//<uses-permission android:name="android.permission.FLASHLIGHT" />
//<uses-feature android:name="android.hardware.camera" />
//<uses-feature android:name="android.hardware.camera.flash" />
ice1000
  • 6,406
  • 4
  • 39
  • 85
Abhishek Sengupta
  • 2,938
  • 1
  • 28
  • 35
  • 1. First create a XML having a switch widget. Then connect it with Java, Using onOffSwitch = (Switch) findViewById(R.id.switch1); 2. Get CameraManager so that u can manage camera's element(which is the flashLight), Using mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); 3.Get current phone's camera ID so that you can instruct android which camera to Point , Using mCameraId = mCameraManager.getCameraIdList()[0]; 4. Turn on using : mCameraManager.setTorchMode(mCameraId, true); 5. Turn off Using : mCameraManager.setTorchMode(mCameraId, false); – Abhishek Sengupta May 25 '18 at 07:24
0

To run some code after a certain delay, you can do the following (where 1000 is the delay in milliseconds):

new Handler().postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        // code to show prompt goes here
    }
}, 1000);

You can make this into a method, which gets called each time the user acknowledges the prompt.

Allen G
  • 1,160
  • 6
  • 8
0

For dialog box check this.

Check this link for flash On/Off.

for Flash Off after one min use Handler.

Handler handler = new Handler();

final Runnable r = new Runnable() {
    public void run() {
       //Turn off flash
    }
};

handler.postDelayed(r, 1000);
Community
  • 1
  • 1
Sanjay Kakadiya
  • 1,596
  • 1
  • 15
  • 32
0
     if(!pk.hasSystemFeature(PackageManager.FEATURE_CAMERA))
            {
                Log.e("err","Device has no camera");
                return;
            }
            camera=camera.open();
            final Parameters p= camera.getParameters();
            btn.setOnClickListener(new OnClickListener()
            {
             @Override
            public void onClick(View v)
             {

                 if(isLightOn)
                 {
                     Log.i("info","FlashLight is turn off");
                     p.setFlashMode(Parameters.FLASH_MODE_OFF);
                     camera.setParameters(p);
                     camera.stopPreview();
                     isLightOn=false;

                 }
                 else
                 {
                     Log.i("info","FlashLight is turn On!");
                     p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                     camera.setParameters(p);
                     camera.startPreview();
                     isLightOn=true;
     new Handler().postDelayed(new Runnable() {
                        // Using handler with postDelayed called runnable run method
                        @Override
                        public void run() {
        //Show your Alert box here
        new AlertDialog.Builder(context) // you can use getApplicationContext() or your activityname.this as context
            .setTitle("Do You Want to continue")
            .setMessage("Are you sure you want to continue?")
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 
                    //Do nothing, Torch continues
                }
             })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 
                    // torch off
 Log.i("info","FlashLight is turn off");
                 p.setFlashMode(Parameters.FLASH_MODE_OFF);
                 camera.setParameters(p);
                 camera.stopPreview();
                 isLightOn=false;
                }
             })
            .setIcon(android.R.drawable.ic_dialog_alert)
             .show();

        }
                    }, 60000); //60000milliseconds = 60 sec = 1min.

       // It will show alertbox after 1 min .

                 }
             }

            });

        }
    }
Mr Robot
  • 1,747
  • 6
  • 35
  • 67
  • I have uploaded the code of my flashlight app can you tell me where I should do the changes – Vikas Kumar Apr 15 '16 at 08:03
  • Okay. Wait. I will help. Please give me some time. – Mr Robot Apr 15 '16 at 08:26
  • Thank you for your help I really appreciate your help but can please also tell me how to generate alert after every 1 minute till the user press on cancel. – Vikas Kumar Apr 16 '16 at 17:46
  • Please help as soon as possible – Vikas Kumar Apr 16 '16 at 18:21
  • I have written a code to generate recursively please have a look at this it is giving only 1 error when the alert box is opening for the first time after 1 min it is giving consecutively 2 alert boxes after that it is working fine and giving only 1 alert box after every min till the user press cancel.I am updating the code – Vikas Kumar Apr 17 '16 at 13:59
  • @VikasKumar: see this link, it will help u. http://stackoverflow.com/q/10029831/4919237 – Mr Robot Apr 17 '16 at 16:35