Device Year Class Library Using Android Application

Introduction

   
This article demonstrates how to add a device year class library using the android studio and XML code.
 
 
Android library that implements a simple algorithm that maps a device's RAM, CPU cores, and clock speed to the year where that combination of specs was considered high end. This allows a developer to easily modify application behavior based on the capabilities of the phone's hardware. 
 
Image1 
 
RAM Condition Year Class
768 1 core 2009
1GB 2+ core 2010
1GB <1.3GHz 2011
1.5GB 1.3GHz 2012
1.5GB <1.8GHz 2013
2GB 1.8GHz 2014
3GB 2.0GHz 2015
more 2.2GHz 2016 & 2017
 
Let's start,
 
Step 1
 
Create a new project in Android Studio from File >> Project and fill all the necessary details.
 
Next, go to Gradle Scripts >> build.gradle (Module: app).Select build.gradle, The app Gradle compile the code and build types will appear. Just replace that the following code. To make a Device year class in your layout XML and add the device year library in your project or you can also Gradle
 
Download the latest JARs or grab via Gradle,
 
Compile Code
  1. compile 'com.facebook.device.yearclass:yearclass:2.0.0'  
or Maven
  1. <dependency>  
  2.   <groupId>com.facebook.device.yearclass</groupId>  
  3.   <artifactId>yearclass</artifactId>  
  4.   <version>2.0.0</version>  
  5. </dependency>  
Step 2
 
Next, go to app >> res >> layout >> activity_main.xml. 
 
image2
 
Select the activity page. The XML code will appear, Just the following code. Create the layout of the TextView and Button.  
 
image3
 
Design View 
 
image4
 
Step 3
 
Next, go to app >> src >> >>main >> java >> MainActivity.java. The jave code will appear. Just replace that with the following jave code 
 
image5
 
Calculate Device Year Class  
 
Calculating the current device's year class is simple
  1. int year = YearClass.get(getApplicationContext());  
Then, later on, you can use the year class to make decisions in your app or send it along with your analytics.
  1. package com.example.ravi.myapplication;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8.   
  9. import com.facebook.device.yearclass.YearClass;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.   
  13.     Button button;  
  14.     TextView textView;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.   
  20.         button = (Button) findViewById(R.id.button);  
  21.         textView = (TextView) findViewById(R.id.textView);  
  22.   
  23.         button.setOnClickListener(new View.OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View view) {  
  26.   
  27.                 int year = YearClass.get(getApplicationContext());  
  28.                 String text;  
  29.   
  30.                 //textView.setText("Current Device year now " + year);  
  31.   
  32.                 if (year >= 2013) {  
  33.                     text= "Do advanced animation";   
  34.                 } else if (year > 2010) {  
  35.                     text = "Do simple animation";  
  36.                 } else {  
  37.                     text = "Phone too slow, don't do any animations";  
  38.                 }  
  39.                 textView.setText("Device year=" + year +"\nDevice capacity="+ text);  
  40.   
  41.             }  
  42.         });  
  43.   
  44.     }  
  45. }  
Next, go to Android Studio and Deploy the application, Select Emulator or your Android mobile with USB debugging enabled. Give it a few sec to make installations and set permission
 
Run the application in your desired emulator (Shift + F10)
 
 
image6 
 

Summary

 
Finally, we have successfully created a Device Year Class application. Later we will discuss more Android Applications.


Similar Articles