2014-04-02 87 views
0

我正在使用StandOutWindow庫。 在我的設置屏幕中,我有一個開關(打開/關閉)。如果我打開開關,彈出窗口和通知顯示。 當我關閉開關時,彈出不顯示。 我想,當我點擊通知時,彈出窗口被隱藏,開關處於「關閉」狀態。現在,我只彈出隱藏。開關也有「ON」狀態。如何關閉開關。 我試圖在getPersistentNotificationIntent寫代碼,但不能。 這裏是我的代碼:在StandOutWindow android中捕獲事件關閉通知?

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); 
     */ 
    } 

} 

回答

0

需要有對傑出服務的單個永久通知,否則Android將殺了你在低內存的服務(這恰好每5秒)。您誤解了每個新窗口都有新的持續通知的事實。除非您有多種類型,否則無論用戶打開多少個窗口,您都只會有一個持久通知。如果存在多種類型問題,請查看FloatingFolders以瞭解如何在單個服務中實現多種類型。