[EDITED]
please ignore what I said earlier - there is much simpler way:
1.
create a new Java class in your package Info.java
package com.your.package.name;
import android.app.Activity;
public class Info {
static Class<? extends Activity> active = null;
}
2. add this line to onResume()
methods of every Activity you have:
Info.active = this.getClass();
3. add these lines to onStop()
methods of every Activity you have:
if(Info.active == this.getClass())
Info.active = null;
4. now for your service:
if(Info.Active != null){
if (Info.active == MainActivity.class){
// code that runs when MainActivity is active HERE
} else if (Info.active == SecondActivity.class){
// code that runs when SecondActivity is active HERE
}
}