在服务中开始阻止对话框

Zul*_*utt 7 android dialog android-alertdialog

阻止Android的所有对话框,这意味着在我的服务运行之前,应用程序或Android系统都不会出现任何对话框.有没有办法以编程方式进行?

Tom*_*Tom 0

我认为不可能仅仅阻止所有弹出窗口。

对我来说,安卓通常不允许这样做是有道理的。

但是您可以尝试(如果您确实想要:))使您的应用程序成为辅助服务,它将对显示的弹出窗口做出反应并立即关闭它。要关闭弹出窗口,您可以在其上找到一些“取消”按钮,然后单击“或” performGlobalAction(GLOBAL_ACTION_BACK);(如果可取消)。

在此处查看一些代码以找到弹出窗口:Android无法使用辅助功能服务在少数设备上读取窗口内容(我不知道这是否有效)

您还可以查看此内容,以获得有关如何使用辅助功能服务查找视图并单击任何应用程序的更多灵感:以编程方式启用/禁用 Android 设备上的辅助功能设置


编辑:更多细节

您需要按照此标准教程将服务添加到您的应用程序:https://developer.android.com/training/accessibility/service.html

首先要注意的是,您应该决定使用 xml 配置并包括android:canRetrieveWindowContent="true"本教程中的内容:

<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"
/>
Run Code Online (Sandbox Code Playgroud)

我想你不需要这条线android:packageName

然后你需要试验回调方法中应该发生什么 - 这只是我的粗略建议:

@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);
}
Run Code Online (Sandbox Code Playgroud)

递归方法可以是这样的

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;
}
Run Code Online (Sandbox Code Playgroud)