Block all dialogs of Android, it means no dialog will appear, either of app or Android systems, until my service is running. Is there a way to do it programmatically?
1 Answers
I don't think it's possible to just block all popups.
For me it makes sense that android doesn't allow that normally.
But you can try (if you really want:) )to make your app an Accessibility Service which will react on popup displayed and immediately close it. To close the popup you can find some Cancel button on it and perform click or performGlobalAction(GLOBAL_ACTION_BACK);
(if its cancelable).
Check out some code here to find a popup: Android unable read window content on few devices using accessibility service (I don't know if that will work)
You can also review this to get some more inspiration on how to find a view and make clicks on any app with the Accessibility Service: Programmatically enabling/disabling accessibility settings on Android device
EDITED: Some more details
You will need to follow this standard tutorial to add the service to your app: https://developer.android.com/training/accessibility/service.html
First thing to note is that you should decide to use xml configuration and include android:canRetrieveWindowContent="true"
like in the tutorial:
<accessibility-service
android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
android:packageNames="com.example.android.myFirstApp, com.example.android.mySecondApp"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:settingsActivity="com.example.android.apis.accessibility.TestBackActivity"
android:canRetrieveWindowContent="true"
/>
and I think you will not need the line android:packageName
Then you need to experiment what should happen in the callback method - here is just my rough suggestion:
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo source = event.getSource();
if(event.getEventType()==AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED)
if(isAlert(source)) //explore the view (maybe recursively) to find if there is an alert
performGlobalAction(GLOBAL_ACTION_BACK);
}
and the recursive method can be like
private boolean isAlert(AccessibilityNodeInfo view){
int count = view.getChildCount();
boolean result = false;
for(int i=0; i<count; i++){
AccessibilityNodeInfo child = view.getChild(i);
if(child.getClassName().contains("Alert")){
return true;
}
if (explore(child));
result = true;
child.recycle();
return result;
}
-
Can you provide some boilerplate/ or helping code to do so , because i can't figure it out yet – Zulqurnain Jutt Aug 20 '16 at 15:09
-
ok, I've just quickly added some code with my idea for your issue. – Tom Aug 23 '16 at 15:35
-
i am still unable to replicate your idea – Zulqurnain Jutt Aug 24 '16 at 13:47