屏蔽Android的所有对话框,这意味着在我的服务运行之前,无论是应用程序还是Android系统,都不会出现对话框。有没有办法以编程方式做到这一点?
最佳答案
我认为仅仅阻止所有弹出窗口是不可能的。
对我来说,android 通常不允许这样做是有道理的。
但是你可以尝试(如果你真的想要:))让你的应用程序成为 Accessibility Service它将对显示的弹出窗口使用react并立即关闭它。要关闭弹出窗口,您可以在其上找到一些取消按钮并执行单击或 performGlobalAction(GLOBAL_ACTION_BACK);
(如果它可取消)。
在此处查看一些代码以查找弹出窗口:Android unable read window content on few devices using accessibility service (不知道行不行)
您还可以查看此内容以获得更多关于如何使用辅助功能服务查找 View 和点击任何应用程序的灵感:Programmatically enabling/disabling accessibility settings on Android device
编辑:更多细节
您需要按照此标准教程将服务添加到您的应用: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"
/>
而且我认为您不需要行 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);
}
递归方法可以像
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;
}
关于android - 开始阻止服务中的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38914581/