When you make Android applications, it is always useful to know the year class of the running device. Indeed, with that information, you can adapt the behavior of your application to the smartphone or tablet of your users. Obviously, the main goal being to offer them a better experience with your application.

 

 

For that, Facebook offers Device Year Class an Android open source library implementing a simple algorithm which maps a device’s RAM, CPU cores and clock speed to the year where those combination of specifications were considered high end.

It allows you to easily modify the behavior of your Android application according the capabilities of the phone’s hardware. The mapping used by the algorithm of Facebook Device Year Class is based on the following table :

In this tutorial, you are going to discover how it is simple to use Facebook Device Year Class Library in your Android applications to detect the probable year class of the running phone.

You can also enjoy directly this tutorial on YouTube in video :

 

Integration

First step is to integrate Facebook Device Year Class Library in your Android application project. For that, you have just to add the following dependency in your build.gradle file :

compile 'com.facebook.device.yearclass:yearclass:2.0.0'

 

Get the year class of the device

Once the library is installed, you can get the year class of the device simply with the following code :

int year = YearClass.get(getApplicationContext());

 

Adapt the behavior of your Android App

With this information, you can modify the behavior of your application with the following snippet of code :

if (year >= 2013) {
    // Do advanced animation
} else if (year > 2010) {
    // Do simple animation
} else {
    // Phone too slow, don't do any animations
}

 

That’s all for that tutorial. To discover more Android tutorials, don’t hesitate to subscribe to the SSaurel’s Channel on YouTube : https://www.youtube.com/user/sylsau/