Follow the top rated answer from this link:
Prevent USSD dialog and read USSD response?
Then add this piece of code in your MainActivity.java under the onCreate section:
Intent openSettings = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
openSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
Once you run your app, the phone will ask you to set the Accessibility setting of your installed app to ON.
If it doesn’t, then manually go to your phone’s settings, under the Accessibility section, set it to ON.