Question

So i designed a app for a basic flashlight it works fine BUT it's slow ineffective and crashes the second you tap the on and off button to fast. what can i do to improve it?

here is what i have so far :

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ToggleButton;
import android.graphics.SurfaceTexture;
import android.hardware.Camera.Parameters;
import android.hardware.Camera;


public class MainActivity extends Activity {

    public Camera cam = Camera.open();
    public Parameters p = cam.getParameters();

    public void onToggleClicked(View view) {
        // Is the toggle on

         ToggleButton button1 = (ToggleButton) findViewById(R.id.toggleButton);

        if (button1.isChecked()) {
            p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_OFF);
            cam.setParameters(p);
            cam.stopPreview();
        } else {
            p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            cam.setParameters(p);
            cam.startPreview();
            }
    }    

    public Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         try {
            cam.setPreviewTexture(new SurfaceTexture(0));
            } catch (Exception e) {
                Log.e("App Log Tag", "ERROR", e);
            e.printStackTrace();
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Log cat errors :

03-24 20:19:01.023: E/Camera3-Device(184): RequestThread: Can't get output buffer, skipping request: Broken pipe (-32)
03-24 20:19:01.023: E/Camera3-OutputStream(184): getBufferLocked: Stream 0: Can't dequeue next output buffer: Broken pipe (-32)
03-24 20:19:01.023: E/Camera3-Device(184): RequestThread: Can't get output buffer, skipping request: Broken pipe (-32)
03-24 20:19:01.023: E/Camera2Client(184): syncWithDevice: Camera 0: Timed out waiting sync with HAL
03-24 20:19:01.073: W/Camera3-OutputStream(184): disconnectLocked: While disconnecting stream 0 from native window, the native window died from under us
03-24 20:19:01.153: E/mm-camera-intf(184): mm_camera_poll_thread_del_poll_fd: invalid handler 175104 (0)
03-24 20:19:01.153: E/mm-camera(196): mct_pipeline_process_set:command=8000009
03-24 20:19:01.153: E/mm-camera(196): mct_pipeline_process_set: stream_type = 1, streamid 2
03-24 20:19:01.153: E/mm-camera-sensor(196): module_sensor_module_process_event:1987 ide 10002 MCT_EVENT_CONTROL_STREAMOFF
03-24 20:19:01.153: E/mm-camera(196): port_isp_mct_ctrl_cmd: E, identity = 0x10002, STREAMOFF
03-24 20:19:01.153: E/mm-camera(196): cpp_module_handle_streamoff_event:2058, info: doing stream-off for identity 0x10002
03-24 20:19:01.153: E/mm-camera(196): module_faceproc_port_event_func:686] FD_STREAMOFF 0 10002
03-24 20:19:01.153: E/mm-camera-img(196): faceproc_comp_abort:608] state 2
03-24 20:19:01.153: E/mm-camera-img(196): Free Wmemory 0
03-24 20:19:01.153: E/mm-camera-img(196): faceproc_comp_abort:635] X
03-24 20:19:01.153: E/mm-camera(196): cpp_module_handle_streamoff_event:2112, info: stream-off done for identity 0x10002
03-24 20:19:01.153: E/mm-camera(196): isp_streamoff: E, session_id = 1, stream_id = 2, stream_type = 1
03-24 20:19:01.153: E/mm-camera(196): isp_axi_util_subscribe_v4l2_event: event_type = 0x8000100, is_subscribe = 0
03-24 20:19:01.153: E/mm-camera(196): isp_axi_util_subscribe_v4l2_event: event_type = 0x8000009, is_subscribe = 0
03-24 20:19:01.233: E/mm-camera(196): ispif_streamoff: session_id = 1, active_streams = 1
03-24 20:19:01.233: E/mm-camera(196): mct_pipeline_process_set: Stream on/off returned
03-24 20:19:01.263: D/QCamera3Stream(184): static void* qcamera::QCamera3Stream::dataProcRoutine(void*): Exit
03-24 20:19:01.263: E/mm-camera-intf(184): mm_camera_poll_thread_del_poll_fd: invalid handler 175616 (0)
03-24 20:19:01.263: E/mm-camera(196): mct_pipeline_process_set:command=8000009
03-24 20:19:01.263: E/mm-camera(196): mct_pipeline_process_set: stream_type = 6, streamid 3
03-24 20:19:01.263: E/mm-camera-sensor(196): module_sensor_module_process_event:1987 ide 10003 MCT_EVENT_CONTROL_STREAMOFF
03-24 20:19:01.263: E/mm-camera(196): port_isp_mct_ctrl_cmd: E, identity = 0x10003, STREAMOFF
03-24 20:19:01.263: E/mm-camera(196): cpp_module_handle_streamoff_event:2058, info: doing stream-off for identity 0x10003
03-24 20:19:01.263: E/mm-camera(196): cpp_module_handle_streamoff_event:2112, info: stream-off done for identity 0x10003
03-24 20:19:01.263: E/mm-camera(196): isp_streamoff: E, session_id = 1, stream_id = 3, stream_type = 6
03-24 20:19:01.263: E/mm-camera(196): isp_axi_util_subscribe_v4l2_event: event_type = 0x8000009, is_subscribe = 0
03-24 20:19:01.363: E/mm-camera(196): stats_action_buf_config: cfg = 0, stats_mask = 0x5b8
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 0, action = 0xb6e7f759
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 1, action = 0xb6e86569
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 2, action = 0xb6e96971
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 3, action = 0xb6e8ff6d
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 4, action = 0xb6e7b819
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 5, action = 0xb6e80225
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 7, action = 0xb6e950e9
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 8, action = 0xb6e92ab1
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 10, action = 0xb6e824c1
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 11, action = 0xb6e761b1
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 12, action = 0xb6e78f19
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 13, action = 0xb6e83ae1
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 14, action = 0xb6e7a051
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 15, action = 0xb6e93509
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 16, action = 0xb6e7d991
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 17, action = 0xb6e77169
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 18, action = 0xb6e8e295
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 19, action = 0xb6e7c4b1
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 20, action = 0xb6e95555
03-24 20:19:01.373: E/mm-camera(196): isp_pipeline_util_reset: module id = 22, action = 0xb6e8b855
03-24 20:19:01.433: E/mm-camera(196): ispif_streamoff: session_id = 1, active_streams = 0
03-24 20:19:01.433: E/mm-camera-sensor(196): module_sensor_module_process_event:2018 ide 10003 MCT_EVENT_CONTROL_STREAMOFF
03-24 20:19:01.433: E/mm-camera(196): mct_pipeline_process_set: Stream on/off returned
03-24 20:19:01.473: D/QCamera3Stream(184): static void* qcamera::QCamera3Stream::dataProcRoutine(void*): Exit
03-24 20:19:01.473: E/mm-camera(196): mct_pipeline_process_set:command=800000b
03-24 20:19:01.473: E/mm-camera(196): mct_pipeline_process_set: stream_type = 1, streamid 2
03-24 20:19:01.473: E/mm-camera-sensor(196): port_sensor_caps_unreserve:169 ide 10002
03-24 20:19:01.473: E/mm-camera(196): cpp_port_check_caps_unreserve:170, identity=0x10002
03-24 20:19:01.473: E/mm-camera(196): cpp_port_check_caps_unreserve:179, identity=0x10002, unreserved
03-24 20:19:01.473: E/mm-camera(196): cpp_port_check_caps_unreserve:170, identity=0x10002
03-24 20:19:01.473: E/mm-camera(196): cpp_port_check_caps_unreserve:179, identity=0x10002, unreserved
03-24 20:19:01.473: E/mm-camera-img(196): faceproc_comp_abort:608] state 1
03-24 20:19:01.473: E/mm-camera-img(196): faceproc_save_album:774] Error No album 
03-24 20:19:01.473: E/mm-camera-img(196): faceproc_comp_eng_destroy: Album save failed -1
03-24 20:19:01.473: E/QCamera3Channel(184): virtual qcamera::QCamera3Channel::~QCamera3Channel(): deleting channel 174849
03-24 20:19:01.473: E/qomx_image_core(184): OMX_Deinit:101] Complete
03-24 20:19:01.473: D/QCamera3PostProc(184): int32_t qcamera::QCamera3PostProcessor::deinit(): Jpeg closed, rc = 0, mJpegClientHandle = 4d00
03-24 20:19:01.473: E/mm-camera(196): mct_pipeline_process_set:command=800000b
03-24 20:19:01.473: E/mm-camera(196): mct_pipeline_process_set: stream_type = 6, streamid 3
03-24 20:19:01.473: E/mm-camera-sensor(196): port_sensor_caps_unreserve:169 ide 10003
03-24 20:19:01.473: E/mm-camera(196): cpp_port_check_caps_unreserve:170, identity=0x10003
03-24 20:19:01.473: E/mm-camera(196): cpp_port_check_caps_unreserve:179, identity=0x10003, unreserved
03-24 20:19:01.473: E/mm-camera(196): q3a_thread_aecawb_stop:308 MSG_STOP_THREAD
03-24 20:19:01.473: E/QCamera3Channel(184): virtual qcamera::QCamera3Channel::~QCamera3Channel(): deleting channel 175362
03-24 20:19:01.473: E/mm-camera(196): mct_pipeline_process_set:command=8000009
03-24 20:19:01.473: E/mm-camera(196): mct_pipeline_process_set: stream_type = 8, streamid 1
03-24 20:19:01.473: E/mm-camera(196): mct_pipeline_process_set: Stream on/off returned
03-24 20:19:01.483: D/QCamera3Stream(184): static void* qcamera::QCamera3Stream::dataProcRoutine(void*): Exit
03-24 20:19:01.483: E/mm-camera(196): mct_pipeline_process_set:command=800000b
03-24 20:19:01.483: E/mm-camera(196): mct_pipeline_process_set: stream_type = 8, streamid 1
03-24 20:19:01.483: E/QCamera3Channel(184): virtual qcamera::QCamera3Channel::~QCamera3Channel(): deleting channel 174336
03-24 20:19:01.483: E/mm-camera(196): mct_pipeline_process_set:command=800000b
03-24 20:19:01.483: E/mm-camera(196): mct_pipeline_get_stream: no children
03-24 20:19:01.483: E/mm-camera(196): mct_pipeline_process_set:1370: Couldn't find stream
03-24 20:19:01.483: E/mm-camera-sensor(196): module_sensor_stop_session:630 session 1
03-24 20:19:01.483: I/PowerManagerService(749): Going to sleep by user request...
03-24 20:19:01.553: E/mm-camera(196): stats_module_stop_session: list =0xaf80cc30, remove port =0xb71a0ba8 name=stats_sink from module=0xb70e3778, name=stats
03-24 20:19:01.553: E/mm-camera(196): stats_module_stop_session: 1 port =0xb71a0ba8 name=stats_sink 
03-24 20:19:01.553: E/mm-camera(196): stats_module_stop_session: 2 port =0xb71a0ba8 name=stats_sink 
03-24 20:19:01.553: I/mm-camera(196): gyro_module_stop_session: Enter
03-24 20:19:01.553: I/mm-camera(196): gyro_module_stop_session: Deinit DSPS
03-24 20:19:01.553: I/mm-camera(196): gyro_module_stop_session: Exit successful
03-24 20:19:01.553: E/mm-camera(196): stats_module_stop_session: 3 port =0xb71a0ba8 name=stats_sink 
03-24 20:19:01.553: E/mm-camera(196): cpp_module_stop_session:453, info: stopping session 1 ...
03-24 20:19:01.553: E/mm-camera(196): cpp_thread_process_pipe_message:359, CPP_THREAD_MSG_ABORT: cpp_thread exiting..
03-24 20:19:01.563: E/mm-camera(196): cpp_module_stop_session:495, info: session 1 stopped.
03-24 20:19:01.563: E/mm-camera(196): c2d_module_stop_session:322, info: stopping session 1 ...
03-24 20:19:01.563: E/mm-camera(196): c2d_module_stop_session:327, info: stopping c2d_thread...
03-24 20:19:01.563: E/mm-camera(196): c2d_module_post_msg_to_thread:381, msg.type=1
03-24 20:19:01.563: E/mm-camera(196): c2d_thread_process_pipe_message:503, C2D_THREAD_MSG_ABORT: c2d_thread exiting..
03-24 20:19:01.563: E/mm-camera(196): c2d_module_stop_session:339, closing c2d subdev...
03-24 20:19:01.563: E/mm-camera(196): c2d_module_stop_session:361, info: session 1 stopped.
03-24 20:19:01.563: I/Camera2Client(184): Camera 0: Closed
03-24 20:19:01.563: I/Camera2ClientBase(184): Closed Camera 0
03-24 20:19:01.953: D/SurfaceFlinger(181): Screen released, type=0 flinger=0xb8658450
03-24 20:19:01.953: D/qdhwcomposer(181): hwc_blank: Blanking display: 0
03-24 20:19:01.983: V/KeyguardHostView(863): Initial transport state: 1, pbstate=2
03-24 20:19:02.073: V/KeyguardHostView(863): show transport, gen:496
03-24 20:19:02.073: V/KeyguardHostView(863): music state changed: 2
03-24 20:19:02.243: D/qdhwcomposer(181): hwc_blank: Done blanking display: 0
03-24 20:19:02.243: D/SurfaceControl(749): Excessive delay in blankDisplay() while turning screen off: 293ms
03-24 20:19:02.273: D/PhoneStatusBar(863): disable: < EXPAND* icons alerts TICKER* system_info BACK* HOME* RECENT* CLOCK* search >
03-24 20:19:02.303: W/IInputConnectionWrapper(863): getExtractedText on inactive InputConnection
03-24 20:19:02.303: W/IInputConnectionWrapper(863): getTextBeforeCursor on inactive InputConnection
03-24 20:19:02.303: W/IInputConnectionWrapper(863): getSelectedText on inactive InputConnection
03-24 20:19:02.303: W/IInputConnectionWrapper(863): getTextAfterCursor on inactive InputConnection
03-24 20:19:02.313: E/InputEventModelImpl(976): onStartInput event aborted: com.touchtype.keyboard.inputeventmodel.ExtractedTextUnavailableException: could not obtain extracted text (class com.touchtype.keyboard.inputeventmodel.ExtractedTextUnavailableException)
03-24 20:19:02.323: D/NfcService(1036): NFC-C OFF
03-24 20:19:10.943: I/art(20002): Heap trim of managed (duration=2.543657ms, advised=796KB) and native (duration=1.751706ms, advised=72KB) heaps. Managed heap utilization of 56%.
03-24 20:19:10.953: I/GCM(20002): GCM message com.google.android.googlequicksearchbox 0:1395706750356778%8b460216f9fd7ecd
03-24 20:19:11.463: D/ConnectivityService(749): handleInetConditionHoldEnd: net=1, condition=100, published condition=100
03-24 20:20:53.503: I/PackageManager(749):   Action: "android.intent.action.SENDTO"
03-24 20:20:53.503: I/PackageManager(749):   Category: "android.intent.category.DEFAULT"
03-24 20:20:53.503: I/PackageManager(749):   Scheme: "sms"
03-24 20:20:53.503: I/PackageManager(749): Adding preferred activity ComponentInfo{com.google.android.talk/com.google.android.apps.babel.phone.BabelHomeActivity} for user 0 :
03-24 20:20:53.523: I/PackageManager(749):   Action: "android.intent.action.SENDTO"
03-24 20:20:53.523: I/PackageManager(749):   Category: "android.intent.category.DEFAULT"
03-24 20:20:53.523: I/PackageManager(749):   Scheme: "smsto"
03-24 20:20:53.523: I/PackageManager(749): Adding preferred activity ComponentInfo{com.google.android.talk/com.google.android.apps.babel.phone.BabelHomeActivity} for user 0 :
03-24 20:20:53.543: I/PackageManager(749):   Action: "android.intent.action.SENDTO"
03-24 20:20:53.543: I/PackageManager(749):   Category: "android.intent.category.DEFAULT"
03-24 20:20:53.543: I/PackageManager(749):   Scheme: "mms"
03-24 20:20:53.543: I/PackageManager(749): Adding preferred activity ComponentInfo{com.google.android.talk/com.google.android.apps.babel.phone.BabelHomeActivity} for user 0 :
03-24 20:20:53.563: I/PackageManager(749):   Action: "android.intent.action.SENDTO"
03-24 20:20:53.563: I/PackageManager(749):   Category: "android.intent.category.DEFAULT"
03-24 20:20:53.563: I/PackageManager(749):   Scheme: "mmsto"
03-24 20:20:53.563: I/PackageManager(749): Adding preferred activity ComponentInfo{com.google.android.talk/com.google.android.apps.babel.phone.BabelHomeActivity} for user 0 :
03-24 20:20:53.693: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (com.google.android.gms.internal.gr@662f66b8)
03-24 20:20:53.723: I/art(29404): Heap trim of managed (duration=18.206399ms, advised=1188KB) and native (duration=5.492916ms, advised=1104KB) heaps. Managed heap utilization of 67%.
03-24 20:20:53.743: W/CursorWrapperInner(29404): Cursor finalized without prior close()
03-24 20:20:53.743: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.d@66251ce8)
03-24 20:20:53.743: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (com.google.android.gms.internal.gr@65ba0ca8)
03-24 20:20:53.753: W/CursorWrapperInner(29404): Cursor finalized without prior close()
03-24 20:20:53.753: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.d@657e9e90)
03-24 20:20:53.753: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (com.google.android.gms.internal.gr@65ce8cb8)
03-24 20:20:53.763: W/CursorWrapperInner(29404): Cursor finalized without prior close()
03-24 20:20:53.763: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.d@66790968)
03-24 20:20:53.763: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (com.google.android.gms.internal.gr@667970d8)
03-24 20:20:53.763: W/CursorWrapperInner(29404): Cursor finalized without prior close()
03-24 20:20:53.763: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.d@6605e2e8)
03-24 20:20:53.763: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (com.google.android.gms.internal.gr@66147400)
03-24 20:20:53.763: W/CursorWrapperInner(29404): Cursor finalized without prior close()
03-24 20:20:53.763: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.d@65f808c8)
03-24 20:20:53.763: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (com.google.android.gms.internal.gr@65f3cd98)
03-24 20:20:53.763: W/CursorWrapperInner(29404): Cursor finalized without prior close()
03-24 20:20:53.763: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.d@6609df98)
03-24 20:20:53.773: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (com.google.android.gms.internal.gr@65f4e930)
03-24 20:20:53.773: W/CursorWrapperInner(29404): Cursor finalized without prior close()
03-24 20:20:53.773: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.d@663df8a0)
03-24 20:20:53.773: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (com.google.android.gms.internal.gr@663e8508)
03-24 20:20:53.773: W/CursorWrapperInner(29404): Cursor finalized without prior close()
03-24 20:20:53.773: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.d@65bd4608)
03-24 20:20:53.773: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (com.google.android.gms.internal.gr@65b29598)
03-24 20:20:53.773: W/CursorWrapperInner(29404): Cursor finalized without prior close()
03-24 20:20:53.773: E/DataHolder(29404): Internal data leak within a DataBuffer object detected!  Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.d@665aafe8)
03-24 20:20:53.843: W/Babel(29404): Media failed to load:Base:content://com.android.contacts/contacts/10 Account=None Load:content://com.android.contacts/contacts/10 key=content://com.android.contacts/contacts/10s192-c-k-no
03-24 20:20:53.883: W/ManagedEGLContext(29404): doTerminate failed: EGL count is 2 but managed count is 1
03-24 20:20:57.573: V/KeyguardServiceDelegate(749): onScreenTurnedOn(showListener = com.android.internal.policy.impl.PhoneWindowManager$17@66480238)
03-24 20:20:57.573: I/PowerManagerService(749): Waking up from sleep...
03-24 20:20:57.583: D/SurfaceFlinger(181): Screen acquired, type=0 flinger=0xb8658450
03-24 20:20:57.583: D/qdhwcomposer(181): hwc_blank: Unblanking display: 0
03-24 20:20:57.623: V/KeyguardServiceDelegate(749): **** SHOWN CALLED ****
03-24 20:20:57.623: I/WindowManager(749): Lock screen displayed!
03-24 20:20:57.813: D/qdhwcomposer(181): hwc_blank: Done unblanking display: 0
03-24 20:20:57.813: D/SurfaceControl(749): Excessive delay in unblankDisplay() while turning screen on: 239ms
03-24 20:21:00.043: D/NfcService(1036): NFC-C ON
03-24 20:21:00.043: I/USERIAL_LINUX(1036): USERIAL_Open(): enter
03-24 20:21:00.043: I/USERIAL_LINUX(1036): USERIAL_Open() device: /dev/bcm2079x port=5, uart_port=0 WAKE_DELAY(20) WRITE_DELAY(20) POWER_ON_DELAY(0) PRE_POWER_OFF_DELAY(10) POST_POWER_OFF_DELAY(0)
03-24 20:21:00.053: I/USERIAL_LINUX(1036): USERIAL_Open(): exit
03-24 20:21:00.053: D/PhoneStatusBar(863): disable: < expand* icons alerts ticker* system_info back* home* recent* clock* search >
03-24 20:21:00.083: W/InputMethodManagerService(749): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@663e5910 attribute=android.view.inputmethod.EditorInfo@663fbc18, token = android.os.BinderProxy@663e5cf8
03-24 20:21:00.133: I/GCM(20002): GCM message com.google.android.googlequicksearchbox 0:1395706815615054%8b460216f9fd7ecd
03-24 20:21:00.633: D/ConnectivityService(749): handleInetConditionHoldEnd: net=1, condition=100, published condition=100
03-24 20:21:01.473: D/audio_hw_primary(184): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2
03-24 20:21:01.483: E/qcom_sensors_hal(749): hal_process_report_ind: Bad item quality: 11 
03-24 20:21:01.493: D/audio_hw_primary(184): select_devices: out_snd_device(2: speaker) in_snd_device(0: )
03-24 20:21:01.553: I/art(749): Heap trim of managed (duration=15.460886ms, advised=8MB) and native (duration=3.325208ms, advised=1456KB) heaps. Managed heap utilization of 49%.
03-24 20:21:01.743: E/SpannableStringBuilder(29404): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-24 20:21:01.743: E/SpannableStringBuilder(29404): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-24 20:21:02.443: I/art(29404): Note: end time exceeds epoch: 
03-24 20:21:03.653: I/GCoreUlr(19985): Successfully inserted location
03-24 20:21:03.653: I/GCoreUlr(19985): Not calling LocationReportingService, hasMoved: false, elapsed millis: 1705539, request: Phone
03-24 20:21:13.363: I/GCM(20002): GCM message com.google.android.googlequicksearchbox 0:1395706873059911%8b460216f9fd7ecd
03-24 20:21:13.503: I/art(749): Heap trim of managed (duration=12.612083ms, advised=8MB) and native (duration=2.613177ms, advised=1480KB) heaps. Managed heap utilization of 48%.
03-24 20:21:13.863: D/ConnectivityService(749): handleInetConditionHoldEnd: net=1, condition=100, published condition=100
03-24 20:21:30.493: E/qcom_sensors_hal(749): hal_process_report_ind: Bad item quality: 11 
03-24 20:21:31.423: I/PowerManagerService(749): Going to sleep due to screen timeout...
03-24 20:21:31.893: D/SurfaceFlinger(181): Screen released, type=0 flinger=0xb8658450
03-24 20:21:31.893: D/qdhwcomposer(181): hwc_blank: Blanking display: 0
03-24 20:21:31.993: D/NfcService(1036): NFC-C OFF
03-24 20:21:32.023: I/art(976): Heap trim of managed (duration=4.792968ms, advised=2MB) and native (duration=4.003073ms, advised=532KB) heaps. Managed heap utilization of 35%.
03-24 20:21:32.033: W/IInputConnectionWrapper(29404): getExtractedText on inactive InputConnection
03-24 20:21:32.033: W/IInputConnectionWrapper(29404): getTextBeforeCursor on inactive InputConnection
03-24 20:21:32.033: W/IInputConnectionWrapper(29404): getSelectedText on inactive InputConnection
03-24 20:21:32.033: W/IInputConnectionWrapper(29404): getTextAfterCursor on inactive InputConnection
03-24 20:21:32.033: E/InputEventModelImpl(976): onStartInput event aborted: com.touchtype.keyboard.inputeventmodel.ExtractedTextUnavailableException: could not obtain extracted text (class com.touchtype.keyboard.inputeventmodel.ExtractedTextUnavailableException)
03-24 20:21:32.183: D/qdhwcomposer(181): hwc_blank: Done blanking display: 0
03-24 20:21:32.183: D/SurfaceControl(749): Excessive delay in blankDisplay() while turning screen off: 285ms
03-24 20:21:36.923: V/KeyguardHostView(863): Initial transport state: 1, pbstate=2
03-24 20:21:36.983: V/KeyguardHostView(863): show transport, gen:496
03-24 20:21:36.983: V/KeyguardHostView(863): music state changed: 2
03-24 20:21:37.013: D/PhoneStatusBar(863): disable: < EXPAND* icons alerts TICKER* system_info BACK* HOME* RECENT* CLOCK* search >
03-24 20:22:45.323: I/Icing.InternalIcingCorporaProvider(8816): Updating corpora: A: SCORES, C: NONE
03-24 20:22:45.513: I/Icing.InternalIcingCorporaProvider(8816): UpdateCorporaTask done [took 188 ms] updated apps [took 188 ms] 
03-24 20:22:46.523: I/Icing(20044): Indexing 52936F3E5A814571A566B6ECEFFA4D84BBC000B6 from com.google.android.googlequicksearchbox
03-24 20:22:46.573: I/Icing(20044): Indexing done 52936F3E5A814571A566B6ECEFFA4D84BBC000B6
03-24 20:22:47.443: W/IdleConnectionHandler(7124): Removing a connection that never existed!
03-24 20:22:47.903: W/IdleConnectionHandler(7124): Removing a connection that never existed!
03-25 16:46:11.143: E/BufferQueue(28408): [unnamed-28408-0] dequeueBuffer: BufferQueue has been abandoned!
03-25 16:46:11.143: E/BufferQueue(28408): [unnamed-28408-0] dequeueBuffer: BufferQueue has been abandoned!
03-25 16:46:11.143: E/BufferQueue(28408): [unnamed-28408-0] dequeueBuffer: BufferQueue has been abandoned!
03-25 16:46:11.143: E/BufferQueue(28408): [unnamed-28408-0] dequeueBuffer: BufferQueue has been abandoned!
03-25 16:46:11.193: I/Choreographer(28408): Skipped 266 frames!  The application may be doing too much work on its main thread.
Was it helpful?

Solution

From what you have posted you should not have any major problems turning the flashlight on or off, so I tried to implement a very simple app with just a toggle button myself and ran it on a Samsung Galaxy Fame to test it out.

But first a few things about your code: You should never make class variables public, unless you really have a good reason to (and even then you should reconsider making them private).

Second: since you are keeping your camera connection open during the active lifetime of your app, you should release your hold on the camera the moment your app is not active - that is when onStop() is called. As a consequence you should not open your camera connection until onStart() is called. Alternatively you can put this control in onCreate() and onDestroy() so that your flashlight can be active when the screen is locked (thus saving power for longer durations and preventing the user from touching anything).

The app I created is very similar to yours, however is does not crash, works very fast, and I literally pushed the toggle button very fast for like 2 mins before stopping. No crashing or lingering happened during that time.

Here is the full code in case you want to test it yourself:

MainActivity.java:

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    private Camera cam;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cam = Camera.open();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void toggleButtonClick(View view){

        Parameters p;

        if(getToggleButton().isChecked()){
            p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            cam.setParameters(p);
            cam.startPreview();
        }
        else{
            p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_OFF);
            cam.setParameters(p);
            cam.stopPreview();
        }
    }

    public ToggleButton getToggleButton(){
        return (ToggleButton) findViewById(R.id.toggleButton1);
    }

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

activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.flashlightapp.MainActivity"
    tools:ignore="MergeRootFrame" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="toggleButtonClick"
        android:text="ToggleButton" />

</FrameLayout>

And then I added these two lines to the Manifest file:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>

I have tested this code, and it works very well on my Samsung Galaxy Fame.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top