Reputation: 55
I am new to android programming and I don't understand why my flashlight is turning off when clicking a button that starts another Activity
with an Intent
? I have searched for this but found nothing similar. I looked at others' ideas but the closest one is to use a Service
instead of an Activity
from my main activity. Is it okay to change my Activity
to a Service
or is another way to resolve this?
tbOnOff = (ToggleButton) findViewById(R.id.togglebutton);
tbOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
if (camera == null) {
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
}
} else {
if (camera != null) {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
}
}
}
});
boolean checkFlash = context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH);
if (!checkFlash) {
tbOnOff.setEnabled(false);
Toast.makeText(context, "LED Not Available!", Toast.LENGTH_LONG)
.show();
}
}
// release camera when onPause called
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
}
}
public void send(View arg1) {
Intent i = new Intent(this, InformationActivity.class);
startActivity(i);
}
that's my code for LED light and the Intent.
Upvotes: 0
Views: 135
Reputation: 8774
When you start a new Activity
it will be brought to the foreground straight away, which means that you're original Activity
will be paused straight away too. Since you're turning off the flashlight in your onPause()
, well that's what it does.
As to your other question, yes, if you want the flashlight to stay on independent of the Activity
lifecycle, a Service
would be a way to do this, as anything else you want to be independent of Activity
lifecycles.
Upvotes: 2