我的 SOS 应用程序仅闪烁一次

My SOS App Is Flashing Light Only Once

我有一个问题,但我没有看到任何解决方案。

我创建了一个 "SOS" 应用程序,它试图使用户 phone 中的手电筒闪烁。

我创建了一些逻辑,这就像

请花 3 分钟时间看一下代码,也许您会发现这里有什么问题。

    public class SOSFragment extends Fragment {

    private Context context;
    private int mInterval = 2000;
    private Handler mHandler = new Handler();
    private Runnable mStatusChecker;
    private boolean isFlashOn = false;
    public SOSFragment() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopRepeatingTask();
    }

    void stopRepeatingTask() {
        mHandler.removeCallbacks(mStatusChecker);
    }

    private void startRepeatingTask() {
        mStatusChecker.run();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sos, container, false);
        Button btn_exit = (Button) view.findViewById(R.id.btn_exit);
        Button btn_sos = (Button) view.findViewById(R.id.btn_sos);
        ((MainActivity) context).checkCameraPermission();
        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity) context).closeSOSFragment();
                ((MainActivity) context).showToolbarFragment();
            }
        });
        btn_sos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startRunnable();
            }
        });
        return view;
    }

    private void startRunnable(){
        mStatusChecker = new Runnable() {
            @Override
            public void run() {
                final Camera cam = Camera.open();
                Camera.Parameters p = cam.getParameters();
                p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                if(!isFlashOn){
                    cam.stopPreview();
                    isFlashOn = true;
                }else {
                    cam.startPreview();
                    isFlashOn = false;
                }

            }
        };
        mHandler.postDelayed(mStatusChecker, mInterval);
        startRepeatingTask();
    }
}

变化:

Runnable startRunnable = new Runnable() {
            public void run() {
                final Camera cam = Camera.open();
                Camera.Parameters p = cam.getParameters();
                p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                if(!isFlashOn){
                    cam.stopPreview();
                    isFlashOn = true;
                }else {
                    cam.startPreview();
                    isFlashOn = false;
                }

            }
        };
        mHandler.postDelayed(this, mInterval);
    }

开始闪烁

mHandler.post(startRunnable);

停止眨眼

mHandler.removeCallbacks(startRunnable);