Yes, with the Android API, you cannot directly retrieve a previously initialized PowerManager.WakeLock by its tag after it has been released or is no longer in scope.
PowerManager.WakeLock instances are managed by the system and are not stored or retrievable by tag once they are released or go out of scope.PowerManager system service does not provide an API to look up or retrieve a WakeLock by its tag.If you need to manage multiple WakeLock instances and refer to them later, you should track them yourself in your application code. For example:
javapublic class WakeLockManager { private static final Map<String, PowerManager.WakeLock> wakeLockMap = new HashMap<>(); public static PowerManager.WakeLock acquireWakeLock(Context context, String tag, int flags) { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = powerManager.newWakeLock(flags, tag); wakeLock.acquire(); wakeLockMap.put(tag, wakeLock); return wakeLock; } public static PowerManager.WakeLock getWakeLock(String tag) { return wakeLockMap.get(tag); } public static void releaseWakeLock(String tag) { PowerManager.WakeLock wakeLock = wakeLockMap.get(tag); if (wakeLock != null && wakeLock.isHeld()) { wakeLock.release(); wakeLockMap.remove(tag); } } }
Store the reference in a field or singleton, so you can release it later.
Map<String, WakeLock>) to manage and retrieve WakeLock instances.