= 11)" I am working on an application that targets API 11 (3.0), but minSDKV...">

Is reflection required if I use "if (android.os.Build.VERSION.SDK_INT> = 11)"

I am working on an application that targets API 11 (3.0), but minSDKVersion is 7 (2.1).

I generate the program PreferenceActivity programmatically, not XML. In Honeycomb in the preferences settings there is a built-in spot for the icon, which can go next to each preference. You can install it using prefScreen.setIcon(R.drawable.my_icon);

Therefore, I do not want to do this according to API 7-10. Is this sufficient protection against failures?

 if (android.os.Build.VERSION.SDK_INT>=11) prefScreen.setIcon(R.drawable.myIcon); 

A more complicated solution that I know is safe is to use reflection to check if this method exists before trying to use it.

+6
source share
3 answers

According to http://developer.android.com/training/basics/activity-lifecycle/starting.html , this implies that it is safe to use the SDK_INT constant on Android 2.0 and higher to transfer calls to new APIs without using reflection.

Note: using SDK_INT to prevent system aging, the new APIs work this way on Android 2.0 (API level 5) and higher only. Previous versions will have a run-time exception.

+10
source

This worked for me:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){ //code } 
+2
source

If this method is not available in lower versions of the platform, it will work when the file is downloaded by the system (this will not even lead to the execution of your if )

You should check out the article on Lazy Loading to be reflected in Android Dev Blog.

+1
source

Source: https://habr.com/ru/post/893989/


All Articles