Support deprecated and new API

645 views Asked by At

Is there any way I can support both deprecated and new API in the same method call for Android? I'm using the camera API which seems to be deprecated for the Lollipop version, so I tried to handle it like this:

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
    {
       //Before Lollipop, use the Camera API since it still supported.
    }
    else
    {
        //Use the CameraManager
        try
        {
            for (int i= 0; i < _camera.getCameraIdList().length; i++)
            {
                System.out.println("Camera= " + _camera.getCameraIdList()[i]);
            }
        }
        catch (CameraAccessException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

But this does however just give me the error Call requires API level 21 (current min is 15): android.hardware.camera2.CameraManager#getCameraIdList I tried SupressLint and TargetApi but that only made the device running an earlier (before Lollipop) Android version crash when creating an class instance of this type.

Thanks for any help!

2

There are 2 answers

0
ianhanniballake On BEST ANSWER

Simply having code in a class does not crash any Android 2.0+ device - code actually needs to run for it to crash. Check to make sure all of your Lollipop specific code is wrapped in version checks.

0
rahul.ramanujam On
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
               //handler lollipop and higher 
            } else {
               //earlier api calls
            }