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

App Freezes and Crashes when trying to Blink the Flashlight

伙计们!这是我的另一个问题..

我正在制作一个手电筒应用程序。在应用程序中有两个按钮,一个用于打开 on/off 闪光灯 (flashlight_switch),另一个用于使闪光灯闪烁 单击按钮 (sos_switch) 以 中速 。闪光灯 on/off 完美运行,但当我按下 SOS 按钮时,应用程序冻结并崩溃。还有我怎样才能关闭 SOS。我是初学者,所以如果您能深入解释答案,那就太好了。如果有任何错别字,请忽略。该应用程序在 Galaxy S3 和 LG G3 上进行了测试,但都没有成功。

完整代码如下:

Java:

手电筒活动:

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 开关也冻结了。我现在想不通。请帮忙!!!!尽快!

You should not sleep Thread.sleep(blinkDelay); the thread because it is main thread need to update UI.you should use a different thread for SOS on

And Your SOS on function is in recursive infinite loop plz edit it. You are calling ON infinite time recursivly

Make Flash class ON / OFF method to public and make few changes in SOS's on method on(); to Flash.on and off to Flash .off

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

}