在StandOutWindow android中捕获事件关闭通知?

时间:2014-04-02 08:50:10

标签: android switch-statement toggle

我正在使用StandOutWindow库。 在我的设置屏幕中,我有一个开关(打开/关闭)。如果我打开开关,则会显示弹出窗口和通知。 当我关闭开关时,弹出窗口不显示。 我想,当我点击通知时,弹出窗口被隐藏,开关有" OFF"状态。现在,我只做隐藏弹出窗口。开关也有" ON"状态。如何关闭开关。 我尝试在getPersistentNotificationIntent中编写代码,但不能。 这是我的代码:

public class TogglePopup extends StandOutWindow {
    public View view;

    @Override
    public String getAppName() {
        return "JSDict";
    }

    @Override
    public int getAppIcon() {
        return android.R.drawable.ic_menu_close_clear_cancel;
    }

    @SuppressLint("NewApi")
    @Override
    public void createAndAttachView(int id, final FrameLayout frame) {
        // create a new layout from body.xml
        final LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.popup_toggle, frame, true);
        ToggleButton toggle = (ToggleButton) view
                .findViewById(R.id.toggle_popup);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                LinearLayout viewLinear = (LinearLayout) view
                        .findViewById(R.id.toggle_popup_background);

                if (isChecked) {
                    // The toggle is enabled
                    viewLinear
                            .setBackgroundResource(R.drawable.popup_toggle_on);

                    ComponentName service = getApplicationContext()
                            .startService(
                                    new Intent(getApplicationContext(),
                                            ClipboardMonitor.class));
                    StandOutWindow.closeAll(getApplicationContext(),
                            ClipboardMonitor.class);
                    StandOutWindow.show(getApplicationContext(),
                            ClipboardMonitor.class, StandOutWindow.DEFAULT_ID);
                } else {
                    // The toggle is disabled
                    viewLinear
                            .setBackgroundResource(R.drawable.popup_toggle_off);

                    getApplicationContext().stopService(
                            new Intent(getApplicationContext(),
                                    ClipboardMonitor.class));
                    StandOutWindow.closeAll(getApplicationContext(),
                            ClipboardMonitor.class);
                }
            }
        });
    }

    // the window will be centered
    @Override
    public StandOutLayoutParams getParams(int id, Window window) {
        return new StandOutLayoutParams(id, 250, 300,
                StandOutLayoutParams.CENTER, StandOutLayoutParams.CENTER);
    }

    // move the window by dragging the view
    @Override
    public int getFlags(int id) {
        return super.getFlags(id) | StandOutFlags.FLAG_BODY_MOVE_ENABLE
                | StandOutFlags.FLAG_WINDOW_FOCUSABLE_DISABLE;
    }

    @Override
    public String getPersistentNotificationMessage(int id) {
        return "Click to close the JSDict";
    }

    @Override
    public Intent getPersistentNotificationIntent(int id) {
        return StandOutWindow.getCloseIntent(this, TogglePopup.class, id);
        /*
         * StandOutWindow.show(this, WidgetsWindow.class,
         * StandOutWindow.DEFAULT_ID);
         */
    }

}

1 个答案:

答案 0 :(得分:0)

StandOut服务需​​要一个持久性通知,否则android会在低内存(每5秒发生一次)时终止你的服务。您错误地认为每个新窗口都有一个新的持久通知。除非您有多种类型,否则无论用户打开多少个窗口,您都只会有一个持久通知。如果存在多种类型的问题,请查看FloatingFolders以了解如何在单个服务中实现多个类型。