Flashlight with strobe: turn flashlight ON when wheel view value is start from 1 to 10 then thread is not stop on zero value

白昼怎懂夜的黑 提交于 2019-12-11 10:32:29

问题


When the strobe value is increased from 1 to 10 the blinking of flashlight will never stop.neither it stops on zero nor it turns on beside it just get blinking and blinking constantly. here i am adding the code which i have written to do so. if someone knows the solutions please help me. Thread is not stop thread works until you stop debugger.. cheers!

enter code here
   flashButton = findViewById(R.id.flash_button);
            mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
            try {
                mCameraId = mCameraManager.getCameraIdList()[0];
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }

            flashButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        if (!isTorchOn) {
                            turnOnFlashLight();

                        } else {
                            turnOffFlashLight();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            scrollValue = findViewById(R.id.value);
            mWheel = findViewById(R.id.wheel);
            mWheel.setOnScrollListener(new Wheel.OnScrollListener() {


                @Override
                public void onScrollStarted(Wheel view, float value, int roundValue) {
                    frequency = Math.abs(roundValue);
                }

                @Override
                public void onScrollFinished(Wheel view, float value, int roundValue) {
                    scrollValue.setText("" + Math.abs(roundValue));
                    frequency = strobeFrequency(Math.abs(roundValue));
                    if(isTorchOn && frequency > 0) {
                       thread = new Thread(stroboRunner);
                         thread = null;
                        strobe(frequency);
                    }
                    else if(isTorchOn) {
                        strobe(0);
                       // thread.stop();
                       // thread=null;
                    }



                }

                @Override
                public void onScroll(Wheel view, float value, int roundValue) {
                    scrollValue.setText("" + Math.abs(roundValue));
                    frequency = strobeFrequency(Math.abs(roundValue));
                    if(isTorchOn && frequency > 0) {
                       // thread = null;
                        strobe(frequency);
                    }

                }
            });

        }


        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        private void turnOnFlashLight() {
    /*
            if(frequency > 0){
                scrollValue.setText("0.0");
            }
    */
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mCameraManager.setTorchMode(mCameraId, true);
                } else {
                    try {
                        camParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        cam.setParameters(camParams);
                        cam.startPreview();
                    } catch (RuntimeException e) {
                        Toast.makeText(this, "Camera Permission is not granted", Toast.LENGTH_SHORT).show();
                    }
                }
                flashButton.setImageResource(R.drawable.power_on);
                isTorchOn = true;
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }


        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        private void turnOffFlashLight() {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if(thread != null){
                        stroboRunner.stopRunning = true;
                        thread = null;
                    }else {
                        mCameraManager.setTorchMode(mCameraId, false);
                    }
                } else {
                    try {
                        if(thread != null){
                            stroboRunner.stopRunning = true;
                            thread = null;
                        }else {
                            camParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                            cam.setParameters(camParams);
                            cam.stopPreview();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(getBaseContext(), "Exception throws in turning off flashlight.", Toast.LENGTH_SHORT).show();
                    }
                }
                flashButton.setImageResource(R.drawable.power_off);
                isTorchOn = false;
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }

        private int strobeFrequency(int roundValue) {
            int freq = 0;
            switch (roundValue) {
                case 1:
                    freq = Math.abs(50);
                    break;
                case 2:
                    freq = Math.abs(150);
                    break;
                case 3:
                    freq = Math.abs(300);
                    break;
                case 4:
                    freq = Math.abs(600);
                    break;
                case 5:
                    freq = Math.abs(900);
                    break;
                case 6:
                    freq = Math.abs(1200);
                    break;
                case 7:
                    freq = Math.abs(1500);
                    break;
                case 8:
                    freq = Math.abs(1800);
                    break;
                case 9:
                    freq = Math.abs(2000);
                    break;
                case 10:
                    freq = Math.abs(2300);
                    break;
            }
            return freq;
        }


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

            try {
                cam = Camera.open();
                camParams = cam.getParameters();
                cam.startPreview();
                hasCam = true;
            } catch (Exception e) {
                // TODO: handle exception
            }

            compass.start();
        }

        @Override
        protected void onPause() {
            super.onPause();
            compass.stop();
        }

        @Override
        protected void onStop() {
            super.onStop();
            Log.d(TAG, "stop compass");
            compass.stop();

        }


       private void strobe(int freq) {
            if(thread == null){
                stroboRunner = new StroboRunner();
                stroboRunner.freq = freq;
                thread = new Thread(stroboRunner);
                thread.start();
            } else if(freq == 0 && thread!=null) {
                stroboRunner.stopRunning = true;
                thread = null;
               // thread.stop();
            }
        }

        private class StroboRunner implements Runnable {
            int freq;
            boolean stopRunning = false;

            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    try {
                        while (!stopRunning) {
                            mCameraManager.setTorchMode(mCameraId, true);
                            Thread.sleep(2600 - freq);
                           System.out.println(""+(2600-freq));
                            // Toast.makeText(MainActivity.this, ""+(2600-freq), Toast.LENGTH_SHORT).show();
                            mCameraManager.setTorchMode(mCameraId, false);
                            Thread.sleep(freq);
                        }
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                  // thread=null;
                } else {
                    Camera.Parameters paramsOn = cam.getParameters();
                    Camera.Parameters paramsOff = camParams;
                    paramsOn.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    paramsOff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    try {
                        while (!stopRunning) {
                            cam.setParameters(paramsOn);
                            cam.startPreview();
                            Thread.sleep(2600 - freq);
                            cam.setParameters(paramsOff);
                            cam.startPreview();
                            Thread.sleep(freq);
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        }

来源:https://stackoverflow.com/questions/47407255/flashlight-with-strobe-turn-flashlight-on-when-wheel-view-value-is-start-from-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!