[Android] obtains information about the mobile phone's hardware and android hardware.

Source: Internet
Author: User

[Android] obtains information about the mobile phone's hardware and android hardware.

Today, I chatted in the QQ group. A buddy bought a 16 GB mobile phone running memory in a certain treasure. At that time, I was so scared, so I had the idea of writing a program to read the actual memory of this mobile phone, so I had this blog today.


Shows all information items. (Some information is displayed blank because my test machine does not have a mobile phone card)



The following code is used:

Package com. liu. chad. practicesqlite; import android. app. activityManager; import android. content. context; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. telephony. telephonyManager; import android. text. format. formatter; import android. util. log; import android. widget. textView; import java. io. bufferedReader; import java. io. fileNotFoundException; import java. io. fileReader; Import java. io. IOException; import java. io. inputStream; public class MainActivity extends ActionBarActivity {private TextView mTextView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mTextView = (TextView) findViewById (R. id. textViewId); getPhoneInfo ();}/*** get mobile phone information */public void getPhoneInfo () {Telepho NyManager tm = (TelephonyManager) this. getSystemService (TELEPHONY_SERVICE); String mtyb = android. OS. build. BRAND; // mobile BRAND String mtype = android. OS. build. MODEL; // mobile phone MODEL String imei = tm. getDeviceId (); String imsi = tm. getSubscriberId (); String numer = tm. getLine1Number (); // mobile phone number String serviceName = tm. getSimOperatorName (); // carrier mTextView. setText ("brand:" + mtyb + "\ n" + "model:" + mtype + "\ n" +" Current: Android "+ android. OS. build. VERSION. RELEASE + "\ n" + "IMEI:" + imei + "\ n" + "IMSI:" + imsi + "\ n" + "mobile phone number: "+ numer +" \ n "+" carrier: "+ serviceName +" \ n "); mTextView. append ("total memory:" + getTotalMemory () + "\ n"); mTextView. append ("current available memory:" + getAvailMemory () + "\ n"); mTextView. append ("CPU name:" + getCpuName () + "\ n"); mTextView. append ("Maximum CPU frequency:" + getMaxCpuFreq () + "\ n"); mTextView. append ("CP U minimum frequency: "+ getMinCpuFreq () +" \ n "); mTextView. append ("Current CPU frequency:" + getCurCpuFreq () + "\ n");}/*** get the phone memory size ** @ return */private String getTotalMemory () {String str1 = "/proc/meminfo"; // system memory information file String str2; String [] arrayOfString; long initial_memory = 0; try {FileReader localFileReader = new FileReader (str1); BufferedReader localBufferedReader = new BufferedReader (localFileReader, 8192); s Tr2 = localBufferedReader. readLine (); // read the first line of meminfo. The total memory size of the system is arrayOfString = str2.split ("\ s +"); for (String num: arrayOfString) {Log. I (str2, num + "\ t");} initial_memory = Integer. valueOf (arrayOfString [1]). intValue () * 1024; // obtain the total system memory, in KB. Multiply by 1024 and convert it to Byte localBufferedReader. close ();} catch (IOException e) {} return Formatter. formatFileSize (getBaseContext (), initial_memory); // Byte to KB or MB, in Memory size normalization}/*** get the current available memory size ** @ return */private String getAvailMemory () {ActivityManager am = (ActivityManager) getSystemService (Context. ACTIVITY_SERVICE); ActivityManager. memoryInfo mi = new ActivityManager. memoryInfo (); am. getMemoryInfo (mi); return Formatter. formatFileSize (getBaseContext (), mi. availMem);} public static String getMaxCpuFreq () {String result = ""; ProcessBuilder cmd; try {Str Ing [] args = {"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq "}; cmd = new ProcessBuilder (args); Process process = cmd. start (); InputStream in = process. getInputStream (); byte [] re = new byte [24]; while (in. read (re )! =-1) {result = result + new String (re);} in. close ();} catch (IOException ex) {ex. printStackTrace (); result = "N/A";} return result. trim () + "Hz" ;}// obtain the Minimum CPU frequency (unit: KHZ) public static String getMinCpuFreq () {String result = ""; ProcessBuilder cmd; try {String [] args = {"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq "}; cmd = new ProcessBuilder (args); Process process = Cmd. start (); InputStream in = process. getInputStream (); byte [] re = new byte [24]; while (in. read (re )! =-1) {result = result + new String (re);} in. close ();} catch (IOException ex) {ex. printStackTrace (); result = "N/A";} return result. trim () + "Hz" ;}// obtain the current CPU frequency in real time (unit: KHZ) public static String getCurCpuFreq () {String result = "N/"; try {FileReader fr = new FileReader ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"); BufferedReader br = new BufferedReader (fr); String text = br. readLine (); result = text. trim () + "Hz";} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return result;} public static String getCpuName () {try {FileReader fr = new FileReader ("/proc/cpuinfo "); bufferedReader br = new BufferedReader (fr); String text = br. readLine (); String [] array = text. split (": \ s +", 2); for (int I = 0; I <array. length; I ++) {} return array [1];} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return null ;}}

The layout file is a TextView and I will not post it here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.