The concrete implementation example of the Android handset mode _android

Source: Internet
Author: User
Tags float range stub

First, the use of distance sensors to monitor the handset near the ear incident
The exact distance sensor does not listen to whether you put your phone close to your ear, maybe you're using your hand in front of the proximity sensor, but that's not our concern, and we're concerned that when your ear is close to the receiver, we want to capture this event.

Step 1, create a new class that implements the Sensoreventlistener interface and implement the Onsensorchanged (Sensorevent event) method

Copy Code code as follows:

public class Mainactivity extends activity implements Sensoreventlistener

Step 2, get the example of the distance sensor

Copy Code code as follows:

Audiomanager = (Audiomanager) This
. Getsystemservice (Context.audio_service);
Msensormanager = (Sensormanager) getsystemservice


(Context.sensor_service);
Msensor = Msensormanager.getdefaultsensor (sensor.type_proximity);

Step 3, in the Onsensorchanged method to determine whether proximity to the handset

Copy Code code as follows:

public void Onsensorchanged (Sensorevent event) {
TODO auto-generated Method Stub
Float range = Event.values[0];


if (range = = Msensor.getmaximumrange ()) {
Toast.maketext (This, "Normal mode", Toast.length_long). Show ();
} else {
Toast.maketext (This, "earpiece mode", Toast.length_long). Show ();
}

Step 4, register the listening distance sensor to the Sensormanager

Copy Code code as follows:

@Override
protected void Onresume () {
Msensormanager.registerlistener (This, msensor,
Sensormanager.sensor_delay_normal);
Super.onresume ();
}
@Override
protected void OnPause () {
Msensormanager.unregisterlistener (this);
Super.onpause ();
}

Second, according to the distance sensor change Audiomanager playback mode
By default, the voice is played with an external speaker or headset, and we need to encode the playback switch.

Step 1, add a permission declaration in the Androidmanifest file

Copy Code code as follows:

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

Step 2, Toggle playback mode

Copy Code code as follows:

public void Onsensorchanged (Sensorevent event) {
TODO auto-generated Method Stub
Float range = Event.values[0];


if (range = = Msensor.getmaximumrange ()) {
Toast.maketext (This, "Normal mode", Toast.length_long). Show ();
Audiomanager.setmode (Audiomanager.mode_normal);
} else {
Toast.maketext (This, "earpiece mode", Toast.length_long). Show ();
Audiomanager.setmode (Audiomanager.mode_in_call);
}

Complete code:

Copy Code code as follows:

Package com.markjoker.sensortest;

Import Java.io.FileInputStream;
Import java.io.IOException;

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.content.Context;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.widget.Toast;

public class Mainactivity extends activity implements Sensoreventlistener {
Private Audiomanager Audiomanager;
Private Sensormanager Msensormanager;
Private Sensor msensor;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Init ();
}

private void init () {
Audiomanager = (Audiomanager) This
. Getsystemservice (Context.audio_service);
Msensormanager = (Sensormanager) getsystemservice (Context.sensor_service);
Msensor = Msensormanager.getdefaultsensor (sensor.type_proximity);

MediaPlayer MPlayer = new MediaPlayer ();
try {
Mplayer.reset ();
Mplayer.setdatasource (New FileInputStream ("/sdcard/snow.mp3"). GETFD ());
Mplayer.prepare ();
Mplayer.start ();
catch (IllegalArgumentException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IllegalStateException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

@Override
protected void Onresume () {
Msensormanager.registerlistener (This, msensor,
Sensormanager.sensor_delay_normal);
Super.onresume ();
}

@Override
protected void OnPause () {
TODO auto-generated Method Stub
Msensormanager.unregisterlistener (this);
Super.onpause ();
}

@Override
public void Onsensorchanged (Sensorevent event) {
TODO auto-generated Method Stub
Float range = Event.values[0];

if (range = = Msensor.getmaximumrange ()) {
Toast.maketext (This, "Normal mode", Toast.length_long). Show ();
Audiomanager.setmode (Audiomanager.mode_normal);
} else {
Toast.maketext (This, "earpiece mode", Toast.length_long). Show ();
Audiomanager.setmode (Audiomanager.mode_in_call);
}
}

@Override
public void onaccuracychanged (Sensor Sensor, int accuracy) {
TODO auto-generated Method Stub

}

}

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.