Android device information, sensor detection

Source: Internet
Author: User

Recently the product has been on the line, began to have time to do their own things, so began to learn and consolidate some previously used or learned technology. Yesterday I wrote a demo to check the serial number and IMEI of the Android device, and so on, to share with you.

Devices are often identified and uniquely identified during development. We used the serial number of the Android device and the Mac number of the device.

First, the acquisition of the device's serial number, in the development of the time we can obtain.

String serialname = Android.os.Build.SERIAL;

You can also use one of the following methods to get the serial number of the device

 Public Static String Getserialnumber () {        null;         Try {            Class<?> c = class.forname ("android.os.SystemProperties");             = C.getmethod ("Get", String.  Class);             = (String) Get.invoke (c, "Ro.serialno");            SYSTEM.OUT.PRINTLN (serial);         Catch (Exception ignored) {        }        return  serial;    }

Second, the Mac that gets the device

string Getmac () {string macserial=NULL; String Str= ""; Try{Process pp=runtime.getruntime (). EXEC ("Cat/sys/class/net/wlan0/address"); InputStreamReader ir=NewInputStreamReader (Pp.getinputstream ()); LineNumberReader input=NewLineNumberReader (IR);  for(;NULL!=str;) {STR=Input.readline (); if(str! =NULL) {macserial= Str.trim ();//Go to Space                     Break; }            }        } Catch(IOException ex) {//give default valuesEx.printstacktrace (); }        returnmacserial; }

Third, get all the sensors inside the device: The sensors included in the Android API have

Type_accelerometer: Acceleration sensor type_gravity: Gravity sensor Type_gyroscope: Gyroscope sensor type_light: Ambient light sensor type_linear_acceleration: Linear Accelerometer Type_magnetic_field: Electromagnetic field sensor type_orientation: direction sensor type_pressure: Pressure sensor type_proximity: distance sensor type_rotation_ Vector: Rotation vector sensor type_temperature: temperature sensor

Here's the code to get the device sensor

// Get the Sensor manager        from System services Sensoramanager Sensormanager = (Sensormanager) Getsystemservice (context.sensor_service); // get the full list        of sensors from the sensor manager List<sensor>sensorlist = sensormanager.getsensorlist (sensor.type_all); // get all the sensors         list<sensor> allsensors = sm.getsensorlist (Sensor.type_all);

Then there is a traversal of all the sensors, and the sensor is differentiated

//shows how many sensors there areMtvsensor.settext ("detected by the phone has" + allsensors.size () + "sensors, they are: \ n"); //display specific information for each sensor         for(Sensor s:allsensors) {String tempstring= "\ n" + "Device Name:" + s.getname () + "\ n" + "Device version:" + s.getversion () + "\ n" + "Supplier:" + S.getvendor () + "\ n"; Switch(S.gettype ()) { CaseSensor.TYPE_ACCELEROMETER:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "acceleration Sensor accelerometer" +tempstring);  Break;  CaseSensor.TYPE_GRAVITY:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Gravity sensor gravity API 9" +tempstring);  Break;  CaseSensor.TYPE_GYROSCOPE:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Gyro Sensor gyroscope" +tempstring);  Break;  CaseSensor.TYPE_LIGHT:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Ambient light sensor" +tempstring);  Break;  CaseSensor.TYPE_LINEAR_ACCELERATION:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Linear accelerator linear_acceleration API 9" +tempstring);  Break;  CaseSensor.TYPE_MAGNETIC_FIELD:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "electromagnetic field sensor magnetic field" +tempstring);  Break;  CaseSensor.TYPE_ORIENTATION:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Direction sensor orientation" +tempstring);  Break;  CaseSensor.TYPE_PRESSURE:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Pressure sensor pressure" +tempstring);  Break;  CaseSensor.TYPE_PROXIMITY:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Distance sensor proximity" +tempstring);  Break;  CaseSensor.TYPE_ROTATION_VECTOR:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Rotation vector rotation" +tempstring);  Break;  CaseSensor.TYPE_TEMPERATURE:mTvSensor.setText (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Temperature Sensor temperature" +tempstring);  Break; default: Mtvsensor.settext (Mtvsensor.gettext (). toString ()+S.gettype ()+ "Unknown sensor" +tempstring);  Break; }        }

Iv. acquiring storage space for the device

//get total device storage space Public Static Longgetsdcardsize () {File path=environment.getexternalstoragedirectory (); StatFs StatFs=NewStatFs (Path.getpath ()); //get the size of a single block of data        LongBlocksize=statfs.getblocksize (); //get the total number of data blocks        Longallblock=Statfs.getblockcount (); return(allblock*blocksize)/1024/1024/1024; }//Get storage space available to your device     Public Static Longgetvialablesdcardsize () {File path=environment.getexternalstoragedirectory (); StatFs StatFs=NewStatFs (Path.getpath ()); //get the size of a single block of data        LongBlocksize=statfs.getblocksize (); //get the number of available data blocks        Longallblock=statfs.getavailableblocks (); return(allblock*blocksize)/1024/1024/1024; }

V. Acquisition of equipment time

 Public Static String SystemTime () {        time time =new time ();        Time.settonow ();         return time.year+ "year" +time.month+ "month" +time.monthday+ "Day" + "Week" +Time.weekday;    

Android device information, sensor detection

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.