Proximity and Wakelock in background service Android

1

I need help to stop a WakeLock in a service. Here is what i have tried:

public class SensorSevice extends Service implements SensorEventListener {
Sensor registerListener;
SensorManager sensorManager;
private PowerManager powerManager;
private PowerManager.WakeLock wakeLock;
private int field = 0x00000020;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub
}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if (event.values[0] == 0) {
        Toast.makeText(this,"Close",Toast.LENGTH_SHORT).show();
        wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
        wakeLock.acquire();
    }else {
        Toast.makeText(this,"Far",Toast.LENGTH_SHORT).show();
        powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
        wakeLock.acquire();
    }
    wakeLock.release();
}

@Override
public void onDestroy() {//here u should unregister sensor
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
    sensorManager.unregisterListener(this);
}

@Override//here u should register sensor and write onStartCommand not onStart
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    registerListener = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    sensorManager.registerListener(this, registerListener, SensorManager.SENSOR_DELAY_NORMAL);
    powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(field, "aa");

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent bIntent = new Intent(SensorSevice.this, MainActivity.class);
    PendingIntent pbIntent = PendingIntent.getActivity(SensorSevice.this, 0, bIntent, 0);
    NotificationCompat.Builder bBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setContentTitle("Title")
                    .setContentText("Subtitle")
                    .setAutoCancel(true)
                    .setOngoing(true)
                    .setContentIntent(pbIntent);
    Notification barNotif = bBuilder.build();
    this.startForeground(1, barNotif);

    return Service.START_STICKY;
}
}

When i use this in a Jellybean device it works. The problem is that it doesn't work in a Nougat device. The service does not turn off the screen but the proximity sensor works. Am i misplacing the wakelock.release?

A reference for the service is in another question found here.

Thank you.

android
wakelock
proximity
asked on Stack Overflow Nov 9, 2017 by Muhammad Badrudin • edited Nov 9, 2017 by Matthew

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0