I want to make an app without UI, which will toggle the flashlight on/off when pressing the app's icon.
I've tried to make a transparent activity which partially works (works when flashlight is off, but when is on and use the app' s icon to turn it off, it turns off but the app crashes with an error dialog).
I'm thinking that the above is not a good way to achive what I want, and probably need to use a service, but I'm new in developing and I don't really know how to code a service.
So, I'm asking you to guide me on which is the best approach for making a toggle flashlight on/off using the app' s icon shortcut, and maybe to give a base example. Thanks in advance.
Here is the code:
public class Flashlight extends Activity {
private boolean isLighOn = false;
private Camera camera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
if (isLighOn) {
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
} else {
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
finish();
};
@Override
public void onPause() {
super.onPause();
bundle.putBoolean("toggleFlashlight", isLighOn);
}
public void onResume() {
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
}
}
}
And here is the logcat error:
E/QualcommCamera( 116): Qint android::get_camera_info(int, camera_info*): E
I/QualcommCameraHardware( 116): Found a matching camera info for ID 0
I/QualcommCameraHardware( 116): HAL_getCameraInfo: orientation = 90
I/QualcommCameraHardware( 116): HAL_getCameraInfo: modes supported = 5
W/CameraService( 116): CameraService::connect X (pid 6147) rejected (existing c
lient).
W/dalvikvm( 6147): threadid=1: thread exiting with uncaught exception (group=0x4
0c911f8)
E/AndroidRuntime( 6147): FATAL EXCEPTION: main
E/AndroidRuntime( 6147): java.lang.RuntimeException: Unable to start activity Co
mponentInfo{com.myprojects.lightsonoff/com.myprojects.lightsonoff.Flashlight}: j
ava.lang.RuntimeException: Fail to connect to camera service
E/AndroidRuntime( 6147): at android.app.ActivityThread.performLaunchActiv.....
I couldn't make it work with this approach, so I created a service in which I started flashlight as a service. Then from the transparent activity I created an if statement which will start the sercvice if not running by checking a blobal boolean flag(which is included in service class), or stop it if already run. The only problem now was that the system kill the flashlight service very easy, so I inclued the startForeground method like this:
Notification fakenote = new Notification( 0, null, System.currentTimeMillis() );
fakenote.flags |= Notification.FLAG_NO_CLEAR;
startForeground( 2, fakenote );
Also I put stopForeground(true) in onDestroy method. Now seems that works fine. Is there any way to prevent system completely from killing the service, so it can be killed only by the app, or this is the best can be done?