Android_ the thief through the sensor

Source: Internet
Author: User

Package Com.beyond.phonestolen;import Android.hardware.sensor;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import Android.hardware.sensormanager;import Android.media.AudioManager; Import Android.media.mediaplayer;import android.os.bundle;import android.app.activity;import android.widget.Toast; public class Mainactivity extends Activity {//Sensor Manager private Sensormanager sm;//distance sensor private sensor sensor;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);//1, get the Sensor manager sm= (Sensormanager) Getsystemservice (Sensor_service) by acquiring system services//2, Obtain the specified distance sensor Sensor=sm.getdefaultsensor (sensor.type_proximity) via the sensor manager,//3, through the Sensor Manager Register listener, Sensing event Sm.registerlistener (new MyListener (), sensor, sensormanager.sensor_delay_game) of the distance sensor;} Private class MyListener implements sensoreventlistener{@Overridepublic void Onsensorchanged (Sensorevent event) {float Distance=event.values[0];//system.out.println (Val[0]); ToaSt.maketext (Mainactivity.this, "" +distance, 0). Show ();//When the phone is inside the trouser pocket, the distance is 0if (distance<1.0f) {return;} When the phone out of the trouser pocket, the distance is 1, play music Playmusic ();} private void Playmusic () {//Play Music MediaPlayer mplayer=mediaplayer.create (Mainactivity.this, r.drawable.alarm); Mplayer.setaudiostreamtype (audiomanager.stream_music); try {mplayer.prepare ();} catch (Exception e) { E.printstacktrace ();} Should theoretically be prepared asynchronously, start the player Mplayer.start () in the prepared callback function;} @Overridepublic void onaccuracychanged (sensor sensor, int accuracy) {}}}

Android_ the thief through the sensor

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.