Application visible

I would like to create an application. The application would run continuously, but it would not be visible. The first is if the application screen becomes visible as a result of some event. Maybe if we shake it. (The magnetic phone case is closed). The second, if the magnetic case is opened, it would go into the background again. My question would be about visibility. How do I solve it?