Wednesday, December 17, 2014

Read system info of Android device

This example show how to read Android system info; such as system build, system properties, memory info, display, system available features and system shared library names.



MainActivity.java
package com.example.androidsysinfo;

import java.util.Enumeration;
import java.util.Properties;

import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.pm.FeatureInfo;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

 TextView textInfo;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textInfo = (TextView)findViewById(R.id.info);
  
  String strInfo = "Build.VERSION.SDK_INT: " + Build.VERSION.SDK_INT + "\n"
   + "Build.BOARD: " + Build.BOARD + "\n"
   + "Build.BOOTLOADER: " + Build.BOOTLOADER + "\n"
   + "Build.BRAND: " + Build.BRAND + "\n"
   + "Build.DEVICE: " + Build.DEVICE + "\n"
   + "Build.DISPLAY: " + Build.DISPLAY + "\n"
   + "Build.FINGERPRINT: " + Build.FINGERPRINT + "\n"
   + "Build.HARDWARE: " + Build.HARDWARE + "\n"
   + "Build.HOST: " + Build.HOST + "\n"
   + "Build.ID: " + Build.ID + "\n"
   + "Build.MANUFACTURER: " + Build.MANUFACTURER + "\n"
   + "Build.MODEL: " + Build.MODEL + "\n"
   + "Build.PRODUCT: " + Build.PRODUCT + "\n"
   + "Build.TAGS: " + Build.TAGS + "\n"
   + "Build.TIME: " + Build.TIME + "\n"
   + "Build.TYPE: " + Build.TYPE + "\n"
   + "Build.USER: " + Build.USER + "\n";
  
  strInfo += "\n - System Properties - \n";
  Properties properties = System.getProperties();
  Enumeration<?> propertyNames = properties.propertyNames();
  while (propertyNames.hasMoreElements()) {
   String key = (String)propertyNames.nextElement();
   String value = properties.getProperty(key);
   strInfo += key + " : " + value + "\n"; 
  }
  
  strInfo += "\n - Memory Info - \n";
  MemoryInfo memoryInfo = new MemoryInfo();
  ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
  activityManager.getMemoryInfo(memoryInfo);
  strInfo += "Total Memory : " + memoryInfo.totalMem + "\n"; //API Level 16
  strInfo += "Available Memory : " + memoryInfo.availMem + "\n";
  strInfo += "Threshold : " + memoryInfo.threshold + "\n";
  strInfo += "Low Memory : " + memoryInfo.lowMemory + "\n";
  
  strInfo += "\n - Display - \n";
  DisplayMetrics metrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(metrics);
  strInfo += "widthPixels : " + metrics.widthPixels + "\n";
  strInfo += "heightPixels : " + metrics.heightPixels + "\n";
  strInfo += "xdpi : " + metrics.xdpi + "\n";
  strInfo += "ydpi : " + metrics.ydpi + "\n";
  strInfo += "density : " + metrics.density + "\n";
  strInfo += "densityDpi : " + metrics.densityDpi + "\n";
  strInfo += "scaledDensity : " + metrics.scaledDensity + "\n";

  strInfo += "\n - SystemAvailableFeatures - \n"; 
  FeatureInfo[] featureInfo  = 
   getPackageManager().getSystemAvailableFeatures();
  for(int i=0; i<featureInfo.length; i++){
   strInfo += featureInfo[i].toString() + "\n";
  }
  
  strInfo += "\n - SystemSharedLibraryNames - \n"; 
  String[] systemSharedLibraryNames = 
   getPackageManager().getSystemSharedLibraryNames();
  for(int i=0; i<systemSharedLibraryNames.length; i++){
   strInfo += systemSharedLibraryNames[i] + "\n";
  }

  textInfo.setText(strInfo);
 }

}

/res/layout/activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidsysinfo.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    
    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView 
                android:id="@+id/info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"/>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

No comments: