Search code examples
androidandroid-5.0-lollipopandroid-4.4-kitkat

Android - Kill an app when screen turned off or screen times out


I want to kill / fully close an app so it doesn’t run even in the background when I press the screen turn on/off button or if the screen times out. I couldn’t find a solution anywhere on the internet. Can you guys help me out with a code snippet? Thanks


Solution

  • you can refer this link to detect screen turn off Screen off Broadcast receiver and for killing the app you can use below code

    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);