Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off screen programmatically when face is close the screen on Android

My app is a dialer and when user holding the phone near his head I need to turn screen off and prevent clicking on the controls - like native Android dialer behavior. What API level I need and how can I do this in right way?

like image 728
Solkin Avatar asked Feb 19 '14 14:02

Solkin


People also ask

How do I turn off screen when not in use?

All you need to do is click on the settings menu from the notification panel or the app drawer and go to the settings icon. Now click on the Display icon. Click on Screen Timeout and click on the Never option. After Clicking on Screen timeout your phone screen will stop turning off.

How do I turn off screen when apps are running?

Edit: Go to settings-> display-> sleep and select a timeout value for the screen. On Android, specifically Motorola Moto G and X your choices should look like the picture I've posted, and go as high as 30 minutes or as low as 15 seconds.

How do I turn my screen on Android?

Method #1: Enable the double-tap gesture to turn on the phone screen. One of the easiest and quickest ways to turn on your Android phone's screen without using the power button is by enabling the double-tap gesture in settings.


2 Answers

I found solution by disassembling one very famous VoIP application. This activity after pressing button1 will disable screen and hardware keys when you close sensors. After pressing button2 this function will be switched off.

Also, this function required permission:

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

Activity. Try it.

public class MainActivity extends Activity {      private Button button1;     private Button button2;     private PowerManager powerManager;     private PowerManager.WakeLock wakeLock;     private int field = 0x00000020;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          try {             // Yeah, this is hidden field.             field = PowerManager.class.getClass().getField("PROXIMITY_SCREEN_OFF_WAKE_LOCK").getInt(null);         } catch (Throwable ignored) {         }          powerManager = (PowerManager) getSystemService(POWER_SERVICE);         wakeLock = powerManager.newWakeLock(field, getLocalClassName());          setContentView(R.layout.main);         button1 = (Button) findViewById(R.id.button1);         button2 = (Button) findViewById(R.id.button2);          button1.setOnClickListener(new View.OnClickListener() {             public void onClick(View v) {                 if(!wakeLock.isHeld()) {                     wakeLock.acquire();                 }             }         });          button2.setOnClickListener(new View.OnClickListener() {             public void onClick(View v) {                 if(wakeLock.isHeld()) {                     wakeLock.release();                 }             }         });     } } 
like image 111
Solkin Avatar answered Oct 17 '22 08:10

Solkin


The following code shows you how to use the proximity sensor:

public class SensorActivity extends Activity implements SensorEventListener { private SensorManager mSensorManager; private Sensor mProximity;   @Override  public final void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);  // Get an instance of the sensor service, and use that to get an instance of // a particular sensor. mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);  }   @Override  public final void onAccuracyChanged(Sensor sensor, int accuracy) { // Do something here if sensor accuracy changes.  }   @Override   public final void onSensorChanged(SensorEvent event) {    float distance = event.values[0]; // Do something with this sensor data.    }   @Override  protected void onResume() { // Register a listener for the sensor. super.onResume(); mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);  }   @Override   protected void onPause() { // Be sure to unregister the sensor when the activity pauses. super.onPause(); mSensorManager.unregisterListener(this);  }} 

try this link for the use of Proximity Sensor while Face is close to screen turn off the screen.

Hope this Helps you.

like image 35
i.n.e.f Avatar answered Oct 17 '22 06:10

i.n.e.f