Does Android use an execution strategy to support different API levels?

I have a little functionality. Turn on the burner and hold it until the user disconnects it from my application or my applications. Using:

            params = camera.getParameters();
            if (params.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)) {
                isFlashOn = true;
                return;
            }
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();

And turn off:

            if (params.getFlashMode().equals(Parameters.FLASH_MODE_OFF)) {
                isFlashOn = false;
                return;
            }
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();

But I notice that it is not very reliable. It works fine for the first time, but after that (especially on my phone the levle 22 API) may not work. I was thinking of testing with android.hardware.camera2 as suggested here. Plan to use if (android.os.Build.VERSION.SDK_INT> 20) and a strategy (base interface implemented by two classes, one using the old API and one new API cameras 2.

? , , ? , , , API, ?

, APK .

, , , Genymotion .

public boolean torchInit() {
    try {
        PackageManager pm = app.getPackageManager();
        // First check if device supports flashlight
        boolean hasFlash = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        if (!hasFlash) {
            Toast.makeText(app, "Flash not found or can\'t get hold of it. No torch", Toast.LENGTH_LONG).show();
            return false;
        }
        camera = Camera.open();
        Camera.Parameters params = camera.getParameters();
        Log.i(LogId, "camera params flash: " + params.getFlashMode());
        return true;
    } catch (Throwable e) {
        Log.e(LogId, "cameraFlashSetup " + e, e);
        Toast.makeText(app, "Flash error, no torch. Description : " + e, Toast.LENGTH_LONG).show();
        camera = null;
    }
    return false;
}

- :

public abstract class TorchInterface  {

protected AppCompatActivity app;

public void init(AppCompatActivity ap){
    app = ap;
}

public abstract boolean torchInit();

public boolean torchReInit(){
    return torchInit();//if re init is not different than init
}

public abstract boolean torchOn();

public abstract boolean torchOff();

}

: , I:

 mBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

:

mBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_MANUAL);

, . , , Camera2Impl :

public boolean torchInit() {
    //do nothing on usual init that app calls on create
    return true;
}

init (flash):

public boolean torchOn() {
        //if not in it, try first 3 times
        if(mBuilder == null || mSession == null){
            if(firstFewTimesTorchOn >  0){
                firstFewTimesTorchOn--;
                torchInit2();
               try{
                    Thread.sleep(150);
                }catch(Exception e){}
                if(mBuilder == null || mSession == null) {
                    return false;
                }
            }
        }
        try {
            mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);//and etc
+4
2

Android "" - . . - , if-else .

- :

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
    mFlashlightStrategy = new PostLollipopStrategy();
} else {
    mFlashlightStrategy = new PreLollipopStrategy();
}
+2

?

, .

context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

true, , false, . .

+2

All Articles