gpt4 book ai didi

android - 检测 DialogFragment 中的用户不活动并在一段时间后注销

转载 作者:太空狗 更新时间:2023-10-29 13:55:34 26 4
gpt4 key购买 nike

我的应用程序中有很多 Activity 和对话 fragment 。我搜索了检测 Activity 并开始了解 onUserInterAction 函数,但它仅在 Activity 中检测 Activity ,而不是在对话框 fragment 打开时检测。

最佳答案

我通过在对话框的窗口回调上创建包装器解决了这个问题。

你的对话框 fragment .java

@Override
public void onStart() {
super.onStart();
UserInteractionInterceptor.wrapWindowCallback(getDialog().getWindow(), getActivity());
}

用户交互拦截器.java

public class UserInteractionInterceptor {

public static void wrapWindowCallback(Window window, FragmentActivity activity) {
Window.Callback originalCallback = window.getCallback();

window.setCallback(new Window.Callback() {
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return originalCallback.dispatchKeyEvent(event);
}

@Override
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
return originalCallback.dispatchKeyShortcutEvent(event);
}
//This is the important override
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if(activity != null)
activity.onUserInteraction();
}
return originalCallback.dispatchTouchEvent(event);
}

@Override
public boolean dispatchTrackballEvent(MotionEvent event) {
return originalCallback.dispatchTrackballEvent(event);
}

@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
return originalCallback.dispatchGenericMotionEvent(event);
}

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
return originalCallback.dispatchPopulateAccessibilityEvent(event);
}

@Nullable
@Override
public View onCreatePanelView(int featureId) {
return originalCallback.onCreatePanelView(featureId);
}

@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
return originalCallback.onCreatePanelMenu(featureId, menu);
}

@Override
public boolean onPreparePanel(int featureId, View view, Menu menu) {
return originalCallback.onPreparePanel(featureId, view, menu);
}

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
return originalCallback.onMenuOpened(featureId, menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
return originalCallback.onMenuItemSelected(featureId, item);
}

@Override
public void onWindowAttributesChanged(WindowManager.LayoutParams attrs) {
originalCallback.onWindowAttributesChanged(attrs);
}

@Override
public void onContentChanged() {
originalCallback.onContentChanged();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
originalCallback.onWindowFocusChanged(hasFocus);
}

@Override
public void onAttachedToWindow() {
originalCallback.onAttachedToWindow();
}

@Override
public void onDetachedFromWindow() {
originalCallback.onDetachedFromWindow();
}

@Override
public void onPanelClosed(int featureId, Menu menu) {
originalCallback.onPanelClosed(featureId, menu);
}

@Override
public boolean onSearchRequested() {
return originalCallback.onSearchRequested();
}

@TargetApi(Build.VERSION_CODES.M)
@Override
public boolean onSearchRequested(SearchEvent searchEvent) {
return originalCallback.onSearchRequested(searchEvent);
}

@Nullable
@Override
public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
return originalCallback.onWindowStartingActionMode(callback);
}

@TargetApi(Build.VERSION_CODES.M)
@Nullable
@Override
public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
return originalCallback.onWindowStartingActionMode(callback, type);
}

@Override
public void onActionModeStarted(ActionMode mode) {
originalCallback.onActionModeStarted(mode);
}

@Override
public void onActionModeFinished(ActionMode mode) {
originalCallback.onActionModeFinished(mode);
}
});
}

关于android - 检测 DialogFragment 中的用户不活动并在一段时间后注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40017937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com