Open top menu


Get All hardware and software Device Information In ListView .For Example:- Android device Storage , SDK Version,Manufacture Name,Model Number ,Serial Number etc


In this blog we will explain how to get android device information programmatically .explain step by step..........

first you can add permission in android manifest file.


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

activity_main.xml
...........................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:tools="http://schemas.android.com/tools"    
android:id="@+id/activity_main"    
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">
    
    
<TextView        
android:layout_width="fill_parent"        
android:layout_height="50dp"        
android:gravity="center"       
 android:textSize="20dp"        
android:textStyle="bold"        
android:textColor="@android:color/white"        
android:background="@color/colorAccent"        
android:text="Hardware/ Software Details"        
android:id="@+id/textView" />

    
<ListView        
android:layout_width="match_parent"        
android:layout_height="match_parent"        
android:layout_below="@+id/textView"        
android:layout_alignParentStart="true"        
android:id="@+id/listDetails"        
android:layout_marginStart="11dp"        
android:layout_marginTop="4dp" />

</RelativeLayout>
...........................................................................................................

list_row.xml
...........................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:orientation="vertical" 
android:layout_width="match_parent"    
android:layout_height="match_parent">

    
<TextView        
android:text="TextView"        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:padding="5dp"        
android:textSize="16dp"        
android:textStyle="bold"        
android:textColor="#066001"        
android:id="@+id/Hardwarename" />

    
<TextView        
android:text="TextView"        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:padding="5dp"        
android:textSize="13dp"        

android:textColor="#c91616"        
android:id="@+id/HardwarenameDetail" />
</LinearLayout>
...........................................................................................................

DeviceInfo.java
...........................................................................................................
public class DeviceInfo {
    private  String HW_SW_Name;
    private  String HW_SW_Info;

    private DeviceInfo(){

    }

    public DeviceInfo(String HW_SW_Name, String HW_SW_Info){
        this.HW_SW_Info=HW_SW_Info;
        this.HW_SW_Name=HW_SW_Name;

    }

    public String getHW_SW_Name() {
        return HW_SW_Name;
    }

    public void setHW_SW_Name(String HW_SW_Name) {
        this.HW_SW_Name = HW_SW_Name;
    }

    public String getHW_SW_Info() {
        return HW_SW_Info;
    }

    public void setHW_SW_Info(String HW_SW_Info) {
        this.HW_SW_Info = HW_SW_Info;
    }
}
...........................................................................................................

DetailListAdapter.java
..........................................................................................................
public class DetailListAdapter  extends BaseAdapter {
    private Context context;

    ArrayList myList = new ArrayList();

    public DetailListAdapter(Context context, ArrayList<DeviceInfo> myList) {
        this.context = context;
        this.myList = myList;
       // this.arraylist = new ArrayList<ReportItem>();        //this.arraylist.addAll(myList);
    }

    @Override    public int getCount() {
        return myList.size();
    }

    @Override    public DeviceInfo getItem(int position) {

        return (DeviceInfo) myList.get(position);
    }

    @Override    public long getItemId(int position) {
        return 0;
    }

    @Override    public View getView(int position, View convertView, ViewGroup parent) {
        DeviceInfo specification = getItem(position);
        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_row, null);
        }
        holder = new ViewHolder();
        try {
            holder.Name = (TextView) convertView.findViewById(R.id.Hardwarename);
            holder.Name.setText(specification.getHW_SW_Name());

            holder.Deatils = (TextView) convertView.findViewById(R.id.HardwarenameDetail);
            holder.Deatils.setText(specification.getHW_SW_Info());

        } catch (Exception e) {
            e.printStackTrace();
        }
        convertView.setTag(holder);
        return convertView;
    }

    static class ViewHolder {

        TextView Name,Deatils ;

    }
}
..........................................................................................................

MainActivity.java
...........................................................................................................
 public class MainActivity extends AppCompatActivity {
    ListView detailList;
    ArrayList<DeviceInfo> DetailList=new ArrayList<>();

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        detailList=(ListView)findViewById(R.id.listDetails);
        try{



       // TelephonyManager tm = (TelephonyManager)getSystemService(this.TELEPHONY_SERVICE);         //   String IMEI=tm.getDeviceId();        ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
        actManager.getMemoryInfo(memInfo);
        long totalMemory = memInfo.totalMem;

      /************************************************************      Insert hardware and software Device Information In ListView       ************************************************************/
        DetailList.add(new DeviceInfo("SERIAL",Build.SERIAL.toString()));
        DetailList.add(new DeviceInfo("MODEL",Build.MODEL.toString()));
        DetailList.add(new DeviceInfo("ID",Build.ID.toString()));
        DetailList.add(new DeviceInfo("MANUFACTURER",Build.MANUFACTURER.toString()));
        DetailList.add(new DeviceInfo("USER",String.valueOf(Build.USER)));
        DetailList.add(new DeviceInfo("BASE",String.valueOf(Build.VERSION_CODES.BASE)));
        DetailList.add(new DeviceInfo("INCREMENTAL",String.valueOf( Build.VERSION.INCREMENTAL)));
        DetailList.add(new DeviceInfo("SDK",String.valueOf(Build.VERSION.SDK)));
        DetailList.add(new DeviceInfo("BOARD",Build.BOARD.toString()));
        DetailList.add(new DeviceInfo("HOST",Build.HOST.toString()));
        DetailList.add(new DeviceInfo("FINGERPRINT",Build.FINGERPRINT.toString()));
        DetailList.add(new DeviceInfo("VERSION CODE",String.valueOf( Build.VERSION.RELEASE)));
        //DetailList.add(new DeviceInfo("IMEI",IMEI));        DetailList.add(new DeviceInfo("TOTAL RAM SIZE",String.valueOf(totalMemory)));
        DetailList.add(new DeviceInfo("USED RAM SIZE",getTotalRAM()));
        DetailList.add(new DeviceInfo("AVAILABLE INTERNAL MEMORY",getAvailableInternalMemorySize()));
        DetailList.add(new DeviceInfo("TOTAL INTERNAL MEMORY",getTotalInternalMemorySize()));
        DetailList.add(new DeviceInfo("AVAILABLE EXTERNAL MEMORY",getAvailableExternalMemorySize()));
        DetailList.add(new DeviceInfo("TOTAL EXTERNAL MEMORY",getTotalExternalMemorySize()));
        DetailList.add(new DeviceInfo("USED RAM SIZE",getTotalRAM()));
        DetailList.add(new DeviceInfo("ANROID ID", Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID)));
        }catch (Exception e){
            e.printStackTrace();
        }

        DetailListAdapter adapter=new DetailListAdapter(
                this,DetailList);
        detailList.setAdapter(adapter);

    }

    public String getTotalRAM() {
        RandomAccessFile reader = null;
        String load = null;
        DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
        double totRam = 0;
        String lastValue = "";
        try {
            reader = new RandomAccessFile("/proc/meminfo", "r");
            load = reader.readLine();

            // Get the Number value from the string            Pattern p = Pattern.compile("(\\d+)");
            Matcher m = p.matcher(load);
            String value = "";
            while (m.find()) {
                value = m.group(1);
                // System.out.println("Ram : " + value);            }
            reader.close();

            totRam = Double.parseDouble(value);
            // totRam = totRam / 1024;
            double mb = totRam / 1024.0;
            double gb = totRam / 1048576.0;
            double tb = totRam / 1073741824.0;

            if (tb > 1) {
                lastValue = twoDecimalForm.format(tb).concat(" TB");
            } else if (gb > 1) {
                lastValue = twoDecimalForm.format(gb).concat(" GB");
            } else if (mb > 1) {
                lastValue = twoDecimalForm.format(mb).concat(" MB");
            } else {
                lastValue = twoDecimalForm.format(totRam).concat(" KB");
            }



        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            // Streams.close(reader);        }

        return lastValue;
    }

    public static boolean externalMemoryAvailable() {
        return android.os.Environment.
                getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
    }

    public static String getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return formatSize(availableBlocks * blockSize);
    }

    public static String getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return formatSize(totalBlocks * blockSize);
    }

    public static String getAvailableExternalMemorySize()
    {
        if (externalMemoryAvailable()) {
            File path = Environment.
                    getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return formatSize(availableBlocks * blockSize);
        } else {
            return ERROR;
        }
    }

    public static String getTotalExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.
                    getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return formatSize(totalBlocks * blockSize);
        } else {
            return ERROR;
        }
    }

    public static String formatSize(long size) {
        String suffix = null;

        if (size >= 1024) {
            suffix = "KB";
            size /= 1024;
            if (size >= 1024) {
                suffix = "MB";
                size /= 1024;
            }
        }

        StringBuilder resultBuffer = new StringBuilder(
                Long.toString(size));

        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }

        if (suffix != null) resultBuffer.append(suffix);
        return resultBuffer.toString();
    }
}
...........................................................................................................



0 comments