如何在选中复选框时发送通知?

How to send a notification when checkbox is checked?

public class Settings extends AppCompatActivity {

CheckBox cb1;

NotificationManager manager;
Notification myNotication;

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


    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// anything here and above seems ok(no error)

    cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)//"this" taskstackbuilder cannot be applied to oncheckedlistener
                    .Builder(mContext)//the mcontext is not working
                    .setContentTitle("New mail from ")
                    .setContentText("i")
                    .setSmallIcon(R.drawable.ic_launcher)
                    .build();Intent resultIntent = new Intent(this, firstpage.class);//everything inside bracket underlined red
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);//"this" taskstackbuilder cannot be applied to oncheckedlistener

        stackBuilder.addParentStack(Settings.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);//cannot resolve symbol mBuilder 
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(mId, mBuilder.build());//cannot resolve symbol mID,mBuilder 

    }//all errors marked


});//basically this,mbuilder,mID,mContext problem

如何让通知和复选框协同工作?

您正在构建通知。但是您还没有通知 NotifcationManager。查看 official docs 中的示例,也许您遗漏了这样的内容?

NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());

将此更改为 Settings.this

使用 mcontext 删除第二个构建器。

与此内部意图相同。将其更改为 Settings.this

TaskStackBuilder.create(this)相同,将其更改为TaskStackBuilder.create(Settings.this)

将此 mBuilder.setContentIntent(resultPendingIntent) 更改为 builder.setContentIntent(resultPendingIntent)

mNotificationManager.notify(mId, mBuilder.build())更改为mNotificationManager.notify(1, builder.build())