我有Service
发起Button
并通过调用WindowManager.addView()
添加它。此按钮始终显示在所有内容之上。
另一个Service
扩展了AccessibilityService
。我确实收到了onAccessibilityEvent
中的活动,但未在另一个Button
中创建的Service
上发生点击事件。我确实收到了其他观点的点击事件。另一个问题是当用户点击在另一个onAccessibilityEvent
中创建的Button
时如何触发Service
?
我不知道为什么Button
中创建的Service
不会触发onAccessibilityEvent
。
这是我的FloatingButton
public class FloatingButton extends Service {
private WindowManager wm;
private WindowManager.LayoutParams mButtonParams;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
mButtonParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mButtonParams.gravity = Gravity.TOP | Gravity.START;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startFloatingButton();
return START_STICKY;
}
private void startFloatingButton() {
Button btn = new Button(this);
btn.setText("scroll");
wm.addView(btn, mButtonParams);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// should fire onAccessibilityEvent()
}
});
}
}
和我的AccessibilityService
public class MyAccessibilityService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.v(TAG, "event type: " + event.getEventType());
}
@Override
public void onInterrupt() {
}
}
我需要在FloatingButton Button
中创建的Service
要么在onAccessibilityEvent
中触发事件,要么在执行AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
的方式中触发。但没有AccessibilityEvent
,我不知道如何执行滚动操作。还有,有没有办法在执行操作之前检查当前视图是否可滚动?
感谢!!!
答案 0 :(得分:0)
更改
mButtonParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
到
mButtonParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
并将其放入您的辅助功能服务的onServiceConnected()
方法中。在辅助功能服务中创建窗口管理器,然后才能获得辅助功能事件。对于向上和向下滚动,
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
TYPE_PHONE不允许后台点击。