In Psalm 66, David makes a clear statement about the relationship of sin and prayer. He says in verse 18:
18 If I regard iniquity in my heart,
The Lord will not hear.
If we walk in sin, God will not hear our prayers. If one thinks about it that makes perfect sense. God being deaf to our prayer when we disobey Him, is only Him respecting our wishes.
Our actions are a more accurate indicator of what we really want, than our words. When we sin, with our feet, by our actions we are saying ” God I don’t want your way, I want my own”. When we do that God respects our wishes. God can only answer prayer according to His will. But we are saying we don’t want His will, so for Him to answer our prayers He would be violating our will, which He wont do.
By not answering our prayer He is only giving us what we really want.
God only ever gives us what we really want.