I making a Flashlight app, and I use Fragments. When I press the button, The lantern light delayed more than 4 seconds, and i don't know what happen. Also, when I Press the switch button another time, the Flashlight doesn't turn off Any idea?
Also I would like to make a stroboscopic lantern light with another button.
I search in internet but i dont find another option to make this feature, only this.
this is my code
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
public class HerramientasFragment extends Fragment {
private Camera cam;
private Switch linterna;
public HerramientasFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
cam = Camera.open();
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View masterView = inflater.inflate(R.layout.fragment_herramientas, container, false);
linterna = (Switch) masterView.findViewById(R.id.switch_linterna);
linterna.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Switch liternaSwitch = (Switch) v;
Parameters p;
if (liternaSwitch.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();
}
}
});
return masterView;
}
}