0

I want to create an application that will turn on and off power saver mode within itself programmatically with on a button click using android-Kotlin. 

I have tried using the intent filter, but it goes to system settings, and then we turn it on or off manually.

code :-

buttonBatterySave.setOnClickListener {
    beep() //sound effect
    val batterySaverIntent = Intent()

    batterySaverIntent.component = ComponentName(
        "com.android.settings",
        "com.android.settings.Settings\$BatterySaverSettingsActivity"
    )
    startActivityForResult(batterySaverIntent, 0)
}

I just want my application to turn on or off by itself within the application with a single button click.

Thank you

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Yashwant
  • 1
  • 1

1 Answers1

0

As of Android 9 the Power management was changed. They use a bucket style procedure that changes according to Android docs. So if you did invoke the power saver in app, then it may not work again on the next app run. So the safest route is to take the user to settings.

You can see the power manager docs here on android developer https://developer.android.com/about/versions/pie/power

To your question is it possible? Yes it is possible but not advised with the new procedure. If your app is in the same bucket that get turned off during power save then your app won’t be able to turn it back on. You would then have to go into settings, power management and then turn the battery saver off in order to use your app again.

This is what Android Docs on power management explains. So the best option is to let the user turn it on off through settings, and leave the system settings alone when it comes to the core programming language of the device.

Camp Nerd
  • 327
  • 1
  • 11