Is it possible to disable Doze mode via adb command executed from Android app?

2.8k Views Asked by At

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>

0

There are 0 best solutions below