First of all, the question too generic and vague to be answered in a proper manner.
However, as a general question, it depends on the use case. Here, I presume you want to execute the 'specific code' basis some trigger.
In terms of Android, this can be a Broadcast event. You might want to implement background tasks, etc. There are a bunch of options you can choose from, you may start reading this: https://developer.android.com/develop/background-work/background-tasks