How to programmatically take a screenshot in Android?

Gist Code: https://gist.github.com/bulentsiyah/beac9bcad49c45177a9769f607c04f13