简体   繁体   中英

How to keep android camera flash on after screen locked

I'm creating a flash light app for android. I'm using service to turn on and off flash. All things are works fine. But when screen locked flash light is automatically turning off and service already running. this happen only device unplugged from charger. when device charging flash light keep turn on after screen locked.How can I avoid this problem.

Here is my FlashLightService.java file

public class FlashLightService extends Service {
    Camera camera;
    Camera.Parameters parameters;
    static boolean isTurnOn;
    static int usedTime = 0;
    static String TAG = "coretorch_service";
Thread t;
static String formattedTime;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "Torch turned on", Toast.LENGTH_SHORT).show();
    isTurnOn = true;

    t =new Thread(new Runnable() {
        @Override
        public void run() {
            while (isTurnOn){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                usedTime++;
            }
            usedTime = 0;
        }
    });

    t.start();

    camera = Camera.open();
    parameters = camera.getParameters();

    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(parameters);
    camera.startPreview();

    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(parameters);
    camera.stopPreview();
    camera.release();
    isTurnOn = false;
    super.onDestroy();
    Toast.makeText(this, "Torch turned off", Toast.LENGTH_SHORT).show();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

public static boolean getFlashStatus() {
    return isTurnOn;
}

public static String getUsedTime(){
    formattedTime = toTimeFormat(usedTime);
    return formattedTime;
}

static String toTimeFormat(int secs){
    String time;
    int mins;
    int seconds;

    if (secs < 60){
        time = String.valueOf(secs) + " sec";
    }
    else {
        mins = (secs / 60);
        seconds = (secs - (mins * 60));
        time = String.valueOf(mins) + " mins " + String.valueOf(seconds) + " sec";
    }

    return time;
 }
  }
@Override
protected void onPause() {
    Camera.open().getParameters().setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
};

Can you do something like this? I have never played with camera

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM