Try this solution,
Create a static variable flag
which is set to true when you receive the broadcast for when screen goes to off
now in your activity
check if the flag is true
@Override
public void onAttachedToWindow() {
if(MyService.Flag == true){
//Continue with your code ...
//....
}else{
finish();
}
}
or do it on onCreate
which ever is suitable for you
Once your screen is unlocked then
//Disable the flag
MyService.Flag = false;
Now when your user clicks the Home
button the activity is called and check again the flag again and if its false then call the finish()
to close the activity