Reputation: 370
I am trying to build an app where camera flash will happen in call and sms incoming activity. But the camera flash is not opening during call...i have written simple program.
public class MainActivity extends Activity {
.....
....
public static class AlertHandler extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("===inside oneceive");
boolean incomingCall = false;
Parameters alertParams;
Camera alertCamera;
String callState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (null != callState && callState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
incomingCall = true;
}
System.out.println("===incomingCall =="+incomingCall);
if(incomingCall){
System.out.println("OPENING CAMERA...");
alertCamera = Camera.open();
alertParams = alertCamera.getParameters();
alertParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
alertCamera.setParameters(alertParams);
alertCamera.startPreview();
}
}
}
}
I am getting the sysout OPENING CAMERA well, but now flash. please help with some code fix. No error in logcat.
In play store lot of this app is present, how they work?
Upvotes: 0
Views: 423
Reputation: 1127
Use this its working
public class MainActivity extends ActionBarActivity {
private static Camera camera;
// flag to detect flash is on or off
private static boolean isLighOn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = Camera.open();
}
public static void toggle() {
if (isLighOn) {
Log.i("info", "torch is turn off!");
final Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
} else {
Log.i("info", "torch is turn on!");
final Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
}
}
public class AlertHandler extends BroadcastReceiver {
boolean incomingCall = false;
@Override
public void onReceive(Context context, Intent intent) {
String callState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (null != callState
&& callState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
incomingCall = true;
}
if (incomingCall) {
MainActivity.toggle();
} else {
MainActivity.toggle();
}
}
}
Upvotes: 1