I have this code
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mProximity;
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
if (event.values[0] == 0) {
//near
Toast.makeText(getApplicationContext(), "near", Toast.LENGTH_SHORT).show();
turnOffScreen();
} else {
//far
Toast.makeText(getApplicationContext(), "far", Toast.LENGTH_SHORT).show();
turnOnScreen();
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void turnOffScreen() {
mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
mWakeLock.acquire();
}
public void turnOnScreen() {
mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
mWakeLock.acquire();
}
}
When the app is sleeping or goes to other activity, the sensor still work.
Any idea?
I took other way to fix this problem, because no one knows how to fixed this, So with this code I don't need to use SensorManager. Now I use two functions for starting and stoping sensor, I think is the only one solution.
import android.content.Context;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
}
public void activateSensor(View v) {
Toast.makeText(MainActivity.this, "Proximity On", Toast.LENGTH_LONG).show();
if (mWakeLock == null) {
mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "incall");
}
if (!mWakeLock.isHeld()) {
Log.d(TAG, "New call active : acquiring incall (CPU only) wake lock");
mWakeLock.acquire();
} else {
Log.d(TAG, "New call active while incall (CPU only) wake lock already active");
}
}
public void deactivateSensor(View v) {
Toast.makeText(MainActivity.this, "Proximity Off", Toast.LENGTH_LONG).show();
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
Log.d(TAG, "Last call ended: releasing incall (CPU only) wake lock");
} else {
Log.d(TAG, "Last call ended: no incall (CPU only) wake lock were held");
}
}
}
And this is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.javier.proximitysensor.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/activity_main_turn_on_screen"
android:onClick="activateSensor"
android:text="@string/activity_main_activate_sensor" />
<Button
android:id="@+id/activity_main_turn_on_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="deactivateSensor"
android:text="@string/activity_main_deactivate_sensor" />
</RelativeLayout>
Thanks...
Also you can check this example in GitHub
In your initial code you have not handled releasing the wake lock properly. When your activity is in onStop , unregister your listener and also release the wake lock held.
Use the function wakeLock.setReferenceCounted(false)
. I have used it, and it works.