Created
September 23, 2021 12:22
-
-
Save prajwal27/1fc95e05263d28c59760a89438cf4491 to your computer and use it in GitHub Desktop.
Android native code for wakelock implementation for Flutter project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lateinit var wakeLock: PowerManager.WakeLock | |
private fun deactivateAutoScreenOffOnProximity() { | |
if(wakeLock.isHeld){ | |
wakeLock.release() | |
} | |
} | |
@SuppressLint("WakelockTimeout") | |
private fun activateAutoScreenOffOnProximity() { | |
if(!wakeLock.isHeld) { | |
wakeLock.acquire() | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
wakeLock= getSystemService(PowerManager::class.java).newWakeLock( | |
PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, | |
"socialupp:wake_lock_for_proximity", | |
) | |
} | |
override fun configureFlutterEngine(flutterEngine: FlutterEngine) { | |
super.configureFlutterEngine(flutterEngine) | |
MethodChannel(flutterEngine.dartExecutor, "channel_name").setMethodCallHandler { call, result -> | |
when (call.method) { | |
"activateAutoScreenOffOnProximity" -> { | |
try { | |
activateAutoScreenOffOnProximity() | |
result.success("Activated auto screen off on proximity") | |
} catch (e: Error) { | |
result.error("", | |
"Couldn't activate auto screen off on proximity ${e.message}", | |
null) | |
} | |
} | |
"deactivateAutoScreenOffOnProximity" -> { | |
try { | |
deactivateAutoScreenOffOnProximity() | |
result.success("Deactivated auto screen off on proximity") | |
} catch (e: Error) { | |
result.error("", | |
"Couldn't deactivate auto screen off on proximity ${e.message}", | |
null) | |
} | |
} | |
else -> result.notImplemented() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment