如果未选中 CheckBox,如何设置 OnGoing(false)?

How to setOnGoing(false) if CheckBox is unchecked?

我想在未选中 CheckBox 时将我的通知方法上的 setOnGoing 设置为 false,但我的代码不起作用。我对 Android 上的通知不熟悉,但我对 Android 和 Java 有点熟悉。 谢谢您的帮助。 (必须根据 Whosebug 的要求添加详细信息)

这是我的代码:

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


}

执行此操作以清除过去的通知

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