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

时间:2017-03-07 19:15:53

标签: android flashlight

我遇到了问题,我没有看到任何解决方案。

我已经创建了一个" SOS"尝试在用户手机上闪烁手电筒的应用程序。

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

一样
  • 当我点击按钮时,只需2秒后启动预览(参见:int mInterval = 2000;

  • 我创建了布尔标志isFlashOn,但这不起作用,我不知道为什么。

请花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();
    }
}

1 个答案:

答案 0 :(得分:1)

<强> 变化:

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);