I'm writing an Android application which is targeted to API level 15 but I also want to keep backward-compatibilty with older API levels (min-sdk 7).
I'm going to reach this by putting conditions deciding which code to use according to current API level (as shown below). I suppose this is a good approach, I just want to ask if it's OK to have so many deprecated methods (like display.getWidth()
), because there is quite many changes between API level 8 and 15.
And if so, if it's a good use of @SuppressWarning("deprecation")
in this case?
Wouldn't it be better to use multiple APKs for example for API levels <= 8 and >= 9? (Even though it's not recommended by developer.android.com.
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point screenSize = new Point();
// calculate the width
float width;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(screenSize);
width = screenSize.x;
} else {
width = display.getWidth();
}
Thanks!
You have to ask yourself a few questions:
In your case, getWidth()
is deprecated in favor of using getSize(Point)
, which requires API level 13. So prior to API level 13, all you have is getWidth()
, which at the time was not deprecated. The reason these deprecated methods are kept around is largely with backwards compatibility in mind (along with avoiding breaking everyone's apps which depend on it).
So to answer your questions, yes, this is fine in this case, and yes, it's a good use of @SuppressWarning("deprecation")
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With