试图闪烁手电筒时应用程序冻结和崩溃

时间:2015-11-15 18:53:57

标签: android flash galaxy led flashlight

有人!这是另一个问题..来自我..

我正在制作手电筒应用。在应用程序中有两个按钮,一个用于打开/关闭闪光灯(flashlight_switch),另一个用于闪烁闪光灯只需轻按一下按钮(sos_switch)中速度即可。闪光灯开/关工作完美,但是当我按下SOS按钮时,应用程序会冻结并崩溃。还有我如何关闭SOS。我是初学者,所以你深入解释答案会很好。如果有,请忽略任何拼写错误。该应用程序在Galaxy S3和LG G3上进行了测试,两者都没有运气。

以下是完整的代码:

爪哇:

FlashlightActivity:

public class FlashlightActivity extends Activity {
    ImageButton flashlight_switch;
    ImageButton sos_switch;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flashlight);
        flashlight_switch = (ImageButton) findViewById(R.id.flashlight_switch);
        sos_switch = (ImageButton) findViewById(R.id.sos_switch);

        flashlight_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

                if (Flash.getTorch()) {
                    flashlight_switch.setImageResource(R.drawable.flashlight_switch_on);
                } else {
                    flashlight_switch.setImageResource(R.drawable.flashlight_switch_off);
                }
            }
        });

        sos_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (SOS.getSOS()) {
                    sos_switch.setImageResource(R.drawable.sos_on);
                } else {
                    sos_switch.setImageResource(R.drawable.sos_off);
                }
            }
        });
    }
}

闪存

class Flash {
private static boolean flashOnOff = false;
private static boolean sosOnOff = false;
public static Camera camera;
private static Camera.Parameters params;


public static boolean getTorch() {
    if (flashOnOff)
        off();
    else
        on();
    return flashOnOff;

}

public static boolean getSOS() {
    if (sosOnOff)
        offSOS();
    else
        onSOS();
    return sosOnOff;

}

private static void on()  {
    if (!flashOnOff) {
        if (camera == null || params == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {
                int a = 10;
            }
        }
        try {
            camera.setPreviewTexture(new SurfaceTexture(0));
            params = camera.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();
            flashOnOff = true;

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

private static void off() {
    if (camera == null || params == null)
        return;
    params = camera.getParameters();
    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    camera.release();
    camera = null;
    flashOnOff = false;
}

private static void onSOS() {
    Thread t = new Thread() {
        public void run() {
            try {

                int delay = 50;
                int times = 10;
                for (int i=0; i < times*2; i++) {
                    if (flashOnOff) {
                        on();
                    } else {
                        off();
                    }
                    sleep(delay);
                }

            } catch (Exception e){
                e.printStackTrace();
            }
        }
    };
    t.start();
}
private static void offSOS() {
    Thread t = new Thread();
    t.stop();}}

提前致谢!

更新

我已经更新了我的flash.java。它现在没有崩溃,但仍然SOS不起作用,而且sos开关也冻结了。我现在无法理解。请帮忙!!!!尽快!

1 个答案:

答案 0 :(得分:1)

  

你不应该睡眠Thread.sleep(blinkDelay);线程,因为主线程需要更新UI。你应该为SOS使用不同的线程

     

你的SOS函数是递归无限循环plz编辑它。你在recursivly上调用无限时间

     

将Flash类的ON / OFF方法公开,并在方法on();上的Flash.onoffFlash .off

的SOS中进行一些更改      

Flash.java

class Flash {
private static boolean flashOnOff = false;
public static Camera camera;
private static Camera.Parameters params;
static Thread t;
    public static boolean getTorch() {
        if (flashOnOff)  // turn off flash
            off();
        else              // turn on flash
            on();
        return flashOnOff;

    }

private static void on() {
    if (!flashOnOff) {
        if (camera == null || params == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {
                int a = 10;
            }
        }
        try {
            params = camera.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.setPreviewTexture(new SurfaceTexture(0));
            camera.startPreview();
            flashOnOff = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

private static void off() {
    if (camera == null || params == null)
        return;
    params = camera.getParameters();
    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    flashOnOff = false;
}
public static void onSOS() {
    t = new Thread() {
        public void run() {
            try {
                int delay = 50;
                while (true) {
                    if (t.isInterrupted())
                        break;
                    getTorch();
                    sleep(delay);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    t.start();

}

public static void offSOS() {
    if (!t.isInterrupted()) {
        t.interrupt();
        off();
    }
}}
  

FlashlightActivity.java

public class FlashlightActivity extends Activity {
ImageButton flashlight_switch;
ImageButton sos_switch;
boolean isStart = false;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flashlight);
        flashlight_switch = (ImageButton) findViewById(R.id.flashlight_switch);
        sos_switch = (ImageButton) findViewById(R.id.sos_switch);
    flashlight_switch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

            if (Flash.getTorch()) {
                flashlight_switch.setImageResource(R.drawable.flashlight_switch_on);
            } else {
                flashlight_switch.setImageResource(R.drawable.flashlight_switch_off);
            }
        }
    });

    sos_switch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!isStart){
        Flash.onSOS();
        isStart = true;
        sos_switch.setImageResource(R.drawable.sos_off);
    }else{
        Flash.offSOS();
        isStart = false;
        sos_switch.setImageResource(R.drawable.sos_on);
    }
        }
    });
}

}