How to get these informations in programmatically code? In this post, I will help you resolve this problem!
1. Information provider classes
- android.os.Build: All information about the current build, extracted from system properties.
- java.lang.System: Provides access to system-related information and resources including standard input and output. All methods of this class are accessed in a static way and the class itself can not be instantiated. By getproperty (String propertyName) method, you can get device System information.
And there are some features from these 2 classes:
Android.os.Build
|
PRODUCT
|
The name of the overall product
|
MODEL
|
The end-user-visible name for the end product
|
|
MANUFACTURE
|
The manufacturer of the product/hardware
|
|
SERIAL
|
A hardware serial number, if available. Alphanumeric only,
case-insensitive
|
|
FINGERPRINT
|
A string that uniquely identifies this build. Do not
attempt to parse this value
|
|
getRadioVersion()
|
Returns the version string for the radio firmware. May
return null (if, for instance, the radio is not currently on)
|
|
Java.lang.System
|
getProperty("os.version")
|
OS (kernel) version
|
getProperty( "os.name")
|
OS (kernel) name
|
|
getProperty("os.arch")
|
OS (kernel) architecture
|
|
...
|
...
|
2. Create an Android Project
Layout for activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/app_name" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/model"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/model"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/device_name"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/device_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/manufacture"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/manufacture"
android:layout_width="0dp"
android:textAllCaps="true"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/screen"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/screen_res"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/version_os"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/version_os"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/serial"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/serial"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/kernel"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/kernel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/baseband"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/baseband"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/build"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/build_num"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/wifi_status"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/wifi_status"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/mobile_network_status"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/mobile_network_status"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/bluetooth"
android:textColor="@android:color/holo_green_dark"
android:textStyle="bold" />
<TextView
android:id="@+id/bluetooth_status"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/holo_blue_dark"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
/**
* Checks if the device has Wifi connection.
*/
public String isWifiNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return "Connected";
} else return "Disconnected";
}
/**
* Checks if the device has 3G or other mobile data connection.
*/
public String isMobileNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected()) {
return "Connected";
} else {
return "Disconnected";
}
Most of Android device support Bluetooth connection, so we can check it's status through checkingBluetoothConnection method:
private String checkingBluetoothConnection () {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
return "Not supported"; //device not support Bluetooth
} else {
if (!mBluetoothAdapter.isEnabled()) {
return "Disabled"; // Bluetooth is disabled
} else {
return "Enabled"; //Bluetooth adapter is anabled and ready to pair with other devices
}
}
}
We also get device screen resolution (Unit: pixels) by using WindowManager:
public String getDeviceScreenResolution() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; //device width
int height = size.y; //device height
return "" + width + " x " + height; //example "480 * 800"
}
Showing all information to TextViews in getDeviceInformation method:
private void getDeviceInformation() {
textDeviceName.setText(Build.PRODUCT);
textModel.setText(Build.MODEL); // get model name
textManufacture.setText(Build.MANUFACTURER); // get device manufacture
textSerial.setText(Build.SERIAL); // get device Serial
textBasebandVersion.setText(Build.getRadioVersion());
textOSVersion.setText(Build.VERSION.RELEASE); // get OS version
textKernelVersion.setText(System.getProperty("os.version")); // get kernel version
textWifiStatus.setText(isWifiNetworkAvailable(this)); // check wifi connection
textMobileNetworkStatus.setText(isMobileNetworkAvailable(this)); //check 3G connection
textBluetoothStatus.setText(checkBluetoothConnection()); //check bluetooth status
textScreenResolution.setText(getDeviceScreenResolution()); //get device resolution
textBuildNumber.setText(Build.FINGERPRINT); //get Device's Build Number
}
3. Final Code
package info.devexchanges.deviceinfor;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.InjectViews;
import butterknife.OnClick;
public class InforActivity extends Activity {
@InjectView(R.id.wifi_status)
TextView textWifiStatus;
@InjectView(R.id.manufacture)
TextView textManufacture;
@InjectView(R.id.model)
TextView textModel;
@InjectView(R.id.device_name)
TextView textDeviceName;
@InjectView(R.id.mobile_network_status)
TextView textMobileNetworkStatus;
@InjectView(R.id.kernel)
TextView textKernelVersion;
@InjectView(R.id.serial)
TextView textSerial;
@InjectView(R.id.baseband)
TextView textBasebandVersion;
@InjectView(R.id.version_os)
TextView textOSVersion;
@InjectView(R.id.build_num)
TextView textBuildNumber;
@InjectView(R.id.screen_res)
TextView textScreenResolution;
@InjectView(R.id.bluetooth_status)
TextView textBluetoothStatus;
/**
* Handling button event
* get device information and show in TextViews
*/
@OnClick(R.id.btn_get)
void onClick() {
getDeviceInformation();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_infor);
//define inject this layout
ButterKnife.inject(this);
}
public String getDeviceScreenResolution() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; //device width
int height = size.y; //device height
return "" + width + " x " + height; //example "480 * 800"
}
private void getDeviceInformation() {
textDeviceName.setText(Build.PRODUCT);
textModel.setText(Build.MODEL); // get model name
textManufacture.setText(Build.MANUFACTURER); // get device manufacture
textSerial.setText(Build.SERIAL); // get device Serial
textBasebandVersion.setText(Build.getRadioVersion());
textOSVersion.setText(Build.VERSION.RELEASE); // get OS version
textKernelVersion.setText(System.getProperty("os.version")); // get kernel version
textWifiStatus.setText(isWifiNetworkAvailable(this)); // check wifi connection
textMobileNetworkStatus.setText(isMobileNetworkAvailable(this)); //check 3G connection
textBluetoothStatus.setText(checkBluetoothConnection()); //check bluetooth status
textScreenResolution.setText(getDeviceScreenResolution()); //get device resolution
textBuildNumber.setText(Build.FINGERPRINT); //get Device's Build Number
}
/**
* Checks if the device has Wifi connection.
*/
public String isWifiNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return "Connected";
} else return "Disconnected";
}
/**
* Checks if the device has 3G or other mobile data connection.
*/
public String isMobileNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected()) {
return "Connected";
} else {
return "Disconnected";
}
}
private String checkBluetoothConnection () {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
return "Not supported";
} else {
if (!mBluetoothAdapter.isEnabled()) {
return "Disabled";
} else {
return "Enabled";
}
}
}
}
For checking Internet connection (network status), Bluetooth status, you must add some permissions in AndroidManifest.xml, so it could be like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.devexchanges.deviceinfor" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".InforActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4. Application output
Install in Android devices, we can see this:
|
|
5. References
- Read my previous post to learn how to use it.
- Go to ButterKnife library page to deep understanding it.
- Thanks JakeWharton for this useful library.
- android.os.Build: http://developer.android.com/reference/android/os/Build.html
- java.lang.System: http://developer.android.com/reference/java/lang/System.html