简体   繁体   English

手电筒怎么闪?

[英]How to blink flashlight?

I've been trying to blink the flashlight of my android for the last 2 hours, and I can't seem to get it to work.在过去的 2 个小时里,我一直试图让我的 android 手电筒闪烁,但我似乎无法让它工作。 All I want is a repeated blink of the flashlight every 500 milliseconds or so, until I click a button.我想要的只是手电筒每 500 毫秒左右重复闪烁一次,直到我单击一个按钮。

Here is what I tried, and it does nothing.这是我尝试过的,它什么也没做。 It doesn't even turn on the flashlight or produce an exception:它甚至不会打开手电筒或产生异常:

   private void blinkLight() {
        cam = Camera.open();
        params = cam.getParameters();

        Thread t = new Thread(){
            public void run(){
            while(userHasntPressedButton){
                params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
                try {
                    currentThread().sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

               }
            }
        };
        t.start();
    }

Most other posts suggest using non-continuous blinking, or using similar to this method, but in a handler, but none are working.大多数其他帖子建议使用非连续闪烁,或使用类似于此方法,但在处理程序中,但没有任何工作。 This was my closest try.这是我最接近的尝试。

How can I blink the flashlight?我怎样才能使手电筒闪烁? Also, is there any way to do it without the camera permission?另外,有没有办法在没有相机许可的情况下做到这一点?

look at this code if you had any query u can ask!如果您有任何疑问,请查看此代码! Use thread class to handle these kind of tasks hope this will help you :) its work for me also thanks!使用线程类来处理这些类型的任务希望这会帮助你 :) 它对我的工作也谢谢!

public void lightBlinking()
  {
    if (blink) {

        flashThread = new Thread(new Runnable() {


            @Override
            public void run() {
                for (int i = 0; i < System.currentTimeMillis(); i++) {

                    if (isFlashOn) {
                        turnOffFlash();<--- // method to OFF flash light

                    } else {
                        turnOnFlash();<--- // method to ON flash light


                    }

                    try {
                            Thread.sleep(1000);<---- // set your time 

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (!blink){  <--- // asign true false for this boolean                     
                                       //when you want to blink your flash
                        flashThread.stop();
                        break;
                    }
                }


            }


    });
    flashThread.start();


    }
}

Try this code instead of using toggle methods, 0 and 1 for ON and OFF Flash Light.试试这个代码,而不是使用切换方法,0 和 1 代表 ON 和 OFF Flash Light。

String myString = "0101010101";
long blinkDelay 500; //Delay in ms
for (int i = 0; i < myString.length(); i++) {
   if (myString.charAt(i) == '0') {
      params.setFlashMode(Parameters.FLASH_MODE_ON);
   } else {
      params.setFlashMode(Parameters.FLASH_MODE_OFF);
   }
   try {
      Thread.sleep(blinkDelay);
   } catch (InterruptedException e) {
      e.printStackTrace();
   }
}

Without the "Thread.sleep()" your code is probably too fast.如果没有“Thread.sleep()”,您的代码可能太快了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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