0

The phone reads through NFC external sensor device. I can create service reading cyclically from NFC. Unfortunately, when the screen is locked, the NFC does not work. I need to unlock the screen for a moment, then it will read NFC and then lock the screen again. How can I do this? I also tried using AlarmManager, but I could not unlock the screen.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

When you are using NFC and you are reading data wake your screen on and when NFC is done reading data use wake your screen off. Use below code

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); wl.acquire();

// screen and CPU will stay awake during this section

wl.release();

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • Hi, I put debug information before release and I can see it. But screen is still black :) I tried to use other options like SCREEN_DIM_WAKE_LOCK, but without luck. Any other ideas? – michcioh Oct 30 '17 at 20:34
0

Correct and working answer is:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

with proper permission in manifest. Source: Turning on screen from receiver/service .