Something like below should work:
View rootView = MainActivity.this.getWindow().getDecorView().getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.measure(MeasureSpec.makeMeasureSpec(this.screenWidth/*specify the width here*/, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(this.screenHeight/*specify the height here*/, MeasureSpec.EXACTLY));
rootView.layout(0, 0, rootView.getMeasuredWidth(), rootView.getMeasuredHeight());
rootView.buildDrawingCache(false);
Bitmap bmScreen = Bitmap.createBitmap(rootView.getDrawingCache());
This code requires the activity to be in memory and in the foreground. Without root access and without modifying Android's source there is no way you can one can take a screenshot. I too researched a lot on it and this was the best I could come up with. In the end we ended up taking the screenshots on Activity's level. However, just in case you're open to root access, here is the link on how to do it.