0

I am using this class from my main activity:

public class MediaInfo {
    public String name;
    public String label;
    // ... (other String and int fields)
    public MediaSession mSession;
}

within an ArrayList:

public class MainActivity extends ActionBarActivity implements AsyncResponse {

    private static final String TAG = "MainActivity";

    ArrayList<MediaInfo> myMediaList;
    private Context mContext;

    public void onCreate(Bundle savedInstanceState) {
        // ...
    }

    // ...
}

The problem I have is that MediaSession is a class that belongs to Android API version 21, and I am trying to make an app from version 14 on.

I would like the activity (or the class) to ignore the field containing the MediaSession class when the version is < 21. The first idea I have come across is using an abstract class and extend it when the version is >= 21, but then I would have to declare different ArrayLists, and I am not sure how I could do it. And maybe this is not the best way...

Any ideas...?

Thank you for your advice

xagutxu
  • 81
  • 6

2 Answers2

0

The problem I have is that MediaSession is a class that belongs to Android API version 21, and I am trying to make an app from version 14 on.

Looking at the Android support library, it seems there is a backward compatible version of MediaSession, called MediaSessionCompat.

user3829751
  • 712
  • 7
  • 20
  • Ok, thank you, I will look for it. Anyway, what's the matter if there isn't a compat version? Is it possible to take into account class fields for a version and not for others...? – xagutxu Aug 04 '15 at 18:26
  • Typically, if it's an essential feature, Google will add a compat version. Otherwise, you'll have to follow Yair's answer and check the device's API level before using the new feature. If API is lower, you'll have to implement the functionality using the old/deprecated way. You can have the newer feature as declared variable, just have to keep in mind to check API level before using it. Hope this helps and happy coding! – user3829751 Aug 04 '15 at 20:53
0

user3829751's answer looks good for this case.

For this generic problem I would lead you to this question: Programmatically obtain the Android API level of a device?

Also check the @TargetApi annotation. You can extract your "version-21" to a method and call it only if the runtime if version 21. You can see an example here: https://stackoverflow.com/a/18449951/3032209

Community
  • 1
  • 1
Yair Kukielka
  • 10,686
  • 1
  • 38
  • 46
  • Thank you, Yair. However, the problem is not how I can obtain the version number (your link explains how easy it is), but how I can manage different classes depending on the version number. – xagutxu Aug 04 '15 at 18:27