Saltar al contenido principal

How to get Device Info in Android

How to Get Device Info in Android.

Getting device information in an Android application can be useful for a variety of reasons. You may need to detect the device's screen size to adjust the UI, or get the device's model and manufacturer to optimize the app's performance. In this article, we will explore the different ways you can get device information in Android.

Step 1: Add Permission to Manifest

Before we can access any device information, we must first add the necessary permission to the app's manifest file. Open the AndroidManifest.xml file and add the following line:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

This permission allows us to access the device's phone state, which includes information like the device's IMEI number and network operator.

Step 2: Use Build Class

The easiest way to get device information is by using the Build class. This class provides several static fields that contain information about the device, such as the device's model, manufacturer, and Android version.

Here's an example of how to get the device's model and manufacturer using Kotlin:

val model = Build.MODEL
val manufacturer = Build.MANUFACTURER

And here's the equivalent code in Java:

String model = Build.MODEL;
String manufacturer = Build.MANUFACTURER;

You can also get other device information using the Build class, such as the Android version:

val androidVersion = Build.VERSION.SDK_INT

Step 3: Use TelephonyManager Class

If you need more advanced device information, such as the device's IMEI number or network operator, you can use the TelephonyManager class. This class provides methods to access information about the device's phone state.

Here's an example of how to get the device's IMEI number using Kotlin:

val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val imei = telephonyManager.imei

And here's the equivalent code in Java:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String imei = telephonyManager.getImei();

Note that the getImei() method is deprecated in Android 10 and higher. You should use the getImei(int slotIndex) method instead, which takes a slot index as a parameter.

More Examples

Example 1: Get Android Device Info - Java

This is a java example on how to get the android device info programmatically. Follow the following steps to recreate the project.

Step 1: Dependencies

No special dependency is needed.

Step 2: Permissions

No special permissions are needed.

Step 3: Design Layout

We have and need only one layout, the layout for our MainActivity.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/view_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.elyeproj.deviceinfo.MainActivity">

</LinearLayout>

Step 4: Write Code

Here is the full code

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private float deviceDensity = 0;
private ViewGroup containerView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

containerView = (ViewGroup)findViewById(R.id.view_container);

calculateDensity();
calculateDeviceInfo();
calculateStatusBar();
}

private void calculateDensity() {
deviceDensity = getResources().getDisplayMetrics().density;

String densityStr = "Undefined";

if (deviceDensity == 0.75) densityStr = "LDPI";
else if (deviceDensity == 1.0) densityStr = "MDPI";
else if (deviceDensity == 1.5) densityStr = "HDPI";
else if (deviceDensity == 2.0) densityStr = "XHDPI";
else if (deviceDensity == 3.0) densityStr = "XXHDPI";
else if (deviceDensity == 4.0) densityStr = "XXXHDPI";

addTextView("Density Value: " + deviceDensity + "(" + densityStr + ")");
}

private void calculateDeviceInfo() {
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);

float dpHeight = outMetrics.heightPixels / deviceDensity;
float dpWidth = outMetrics.widthPixels / deviceDensity;

addTextView("Height Resolution(dp): " + dpHeight);
addTextView("Width Resolution(dp): " + dpWidth);
}

private void calculateStatusBar() {
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
float dpStatusBar = getResources().getDimensionPixelSize(resourceId)/deviceDensity;
addTextView("Status Bar Resolution(dp)\t: " + dpStatusBar);
}
}

private void addTextView(String text) {
TextView textView = new TextView(this);
textView.setText(text);
containerView.addView(textView);
}
}

Reference

Download the code below.

NumberLink
1.Download code
2.Follow code author

Conclusion

Getting device information in Android is essential for creating a better user experience and optimizing the app's performance. In this article, we explored two ways to get device information: using the Build class and the TelephonyManager class. Now that you know how to get device information, you can start using it to create better Android apps.