2015-05-28 34 views
0

我想setOnGoing我的通知方法爲false時CheckBox未選中,但我的代碼不起作用。我對Android上的通知很陌生,但我對Android和Java有點熟悉。 感謝您的幫助。 (必須添加詳細的StackOverflow的要求吧)如果CheckBox未被選中,如何設置OnGoing(false)?

這裏是我的代碼:

package com.arvisapps.lazyscreenshot; 

import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.os.Bundle; 
import android.support.v4.app.NotificationCompat; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 


public class MainActivity extends Activity { 


NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

private void showNotification() { 
    builder.setAutoCancel(false); 
    builder.setContentTitle("Take Screenshot"); 
    builder.setContentText(""); 
    builder.setSmallIcon(R.drawable.ic_notification); 
    builder.setOngoing(true); 

    Notification notification = builder.build(); 
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(1, notification); 
} 

private void dontShowNotification(){ 
    builder.setOngoing(false); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    CheckBox cb1 = (CheckBox) findViewById(R.id.cb_1); 

    cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

      if (isChecked == true) { 
       showNotification(); 
      } 

      else if(isChecked == false) 
      { 
       dontShowNotification(); 
      } 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


} 
+0

對不起永記我看你要刪除的通知? – Xjasz

+0

是的,這是我想要的,但即使它不取消後不刷卡。 – 4127157

+0

告訴我,如果該作品 – Xjasz

回答

1

這樣做是爲了清除過期通知

NotificationManager nMgr = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
nMgr.cancel(notifyId); <-----this integer needs to be the same as the one shown. 

只需運行相同的語句,但有假。如果使用相同的整數,它將替換舊的通知。

private void dontShowNotification(){ 
    builder.setOngoing(false); 
    builder.setAutoCancel(false); 
    builder.setContentTitle("Take Screenshot"); 
    builder.setContentText(""); 
    builder.setSmallIcon(R.drawable.ic_notification); 
    Notification notification = builder.build(); 
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(1, notification); 
} 

更好的方法

private void showNotification(boolean b) { 
    builder.setAutoCancel(false); 
    builder.setContentTitle("Take Screenshot"); 
    builder.setContentText(""); 
    builder.setSmallIcon(R.drawable.ic_notification); 
    builder.setOngoing(b); 

    Notification notification = builder.build(); 
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(1, notification); 
} 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    CheckBox cb1 = (CheckBox) findViewById(R.id.cb_1); 

    cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      showNotification(isChecked); 
     } 
    } 
}); 
} 
+0

它正在工作。你能告訴我如何取消它取消它?謝謝 – 4127157

+0

它每次都替換舊的通知。如果你改變了manager.notify(1,通知);給這位manager.notify(2,通知);它只會讓其中的2個 – Xjasz

+0

是的,我知道的。我想說的是,當我檢查框時,它應該顯示一個不可移動的通知。但是,如果未經檢查,通知應該消失。 – 4127157

相關問題