How to disable doze mode in android, kotlin?

To disable doze mode in an Android device, you can follow these steps:

  1. Open the Settings app on your Android device.
  2. Scroll down and tap on the “Battery” or “Battery & performance” option.
  3. Tap on the “Battery optimization” option.
  4. Tap on the “Not optimized” drop-down menu and select “All apps”.
  5. Scroll down and find the app for which you want to disable doze mode.
  6. Tap on the app and select “Don’t optimize”.

Alternatively, you can disable doze mode for specific apps by using the following code in Kotlin:

val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
val packageName = "com.example.myapp"
val packageIntent = packageManager.getPackageInfo(packageName, 0)
powerManager.setDozeWhitelistApp(packageIntent.packageName, true)

Note that this code is intended for use in an Activity class. You may need to modify it slightly depending on your specific use case. Additionally, keep in mind that disabling doze mode can have negative effects on battery life, so it should only be done if absolutely necessary.

0 0 votes
Article Rating

Leave a Reply

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x