0

I have a requirement to disable the doze mode completely on my phone from my android app. I was able to find that the following command can disable Doze mode in Andriod.

adb -d shell dumpsys deviceidle disable

I made a small sample to run the above command from my Android application. I am getting an exception - permission denied.

package com.example.myapplication

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.io.BufferedReader
import java.io.InputStreamReader

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_test_shell.setOnClickListener()
        {
            val adbShellCommand =   "dumpsys deviceidle disable" //"pwd" //

            val pb = ProcessBuilder(adbShellCommand)
            val p: Process

            try {
                p = pb.start()
                val reader = BufferedReader(InputStreamReader(p.inputStream))
                //Set the output to a TextView
                txt_shell_output.text = reader.read().toString()

                p.waitFor()
                p.destroy()
            } catch (e: Exception) {
                e.printStackTrace()
                Log.e("Execute Shell",  e.printStackTrace().toString())
            }
        }
    }

}

When I run the same code using Runtime.getRuntime().exec(adbShellCommand), I get an output of 80 in the TextView, not sure what it means. In this case, there is no exception though.

Few questions:

  1. Are we allowed to run the above command, from within an Android app?

  2. Does whitelisting your app via the following command, result in the same effect as setting the 'Ignore Battery Optimizations' in Android? adb -d shell dumpsys deviceidle whitelist + <pkg-name>

Deepak V
  • 299
  • 2
  • 15
  • Have you tried using wakelocks? here is the documentation about it in case you didn't heard before https://developer.android.com/training/scheduling/wakelock – Luciano Ferruzzi Sep 01 '20 at 03:36
  • Hello @LucianoFerruzzi, yes we already tried wake-locks. However, that is having a side effect of waking the phone screen every few minutes and it is annoying to the end-user. – Deepak V Sep 01 '20 at 06:30
  • We need to use BLE in our app and have to do background scan and advertising. when the device enters doze mode, the OS restricts access to peripherals. – Deepak V Sep 01 '20 at 06:55
  • Partial wake locks ensure that the CPU is on, and allow the display to go off: https://developer.android.com/reference/android/os/PowerManager#PARTIAL_WAKE_LOCK – Luciano Ferruzzi Sep 01 '20 at 23:39
  • Also, there are other ways to add the app to whitelist check this: https://stackoverflow.com/questions/32627342/how-to-whitelist-app-in-doze-mode-android-6-0 – Luciano Ferruzzi Sep 01 '20 at 23:39

0 Answers0