I want my app to execute a piece of code whenever the homebutton is pressed. This should also happen, when the app itself isn't currently open. I have found a way to do this with volume buttons:
How can I launch an android app on upon pressing the volume up or volume down button?
But I need it to work with the home button. Setting the app as a launcher doesn't work, as it won't work on the lockscreen then.