进度通知未被取消

时间:2017-05-21 10:49:51

标签: java android notifications

当我按下"取消"通知中的按钮,通知仍然保留在通知栏中,每次按"取消"通知已关闭但仍显示在顶部栏中,当我打开通知时仍然存在,通知未正确删除。

这是创建进度通知的代码,它运行正常:

private void createProgressNotification() {

    PendingIntent dismissIntent = NotificationActivity.getDismissIntent(NOTIFICATION_PROGRESS, Analyse.this);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setContentTitle("URL Analysis")
                    .setContentText("Analysis in progress")
                    .setSmallIcon(R.drawable.icon_notification)
                    .setAutoCancel(false)
                    .addAction(R.drawable.cancel, "Cancel", dismissIntent)
                    .setOngoing(true)
                    .setProgress(0, 0, true);

    NotificationManager mNotifyManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyManager.notify(NOTIFICATION_PROGRESS, mBuilder.build());
}

这是我必须处理dismissIntent的自定义NotificationActivity类:

public class NotificationActivity extends Activity {

    public static final String NOTIFICATION_ID = "NOTIFICATION_ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
        finish(); // since finish() is called in onCreate(), onDestroy() will be called immediately
    }

    public static PendingIntent getDismissIntent(int notificationId, Context context, AsyncCallWS analyze) {
        Intent intent = new Intent(context, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(NOTIFICATION_ID, notificationId);
        PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        return dismissIntent;
    }

}

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

在NotificationCompat Builder属性

的setAutoCancel()中将false更改为true
string name = textBox2.Text;
string insert = "insert into details values('"+name+"')";

SqlCommand cmd = new SqlCommand(insert, con);

cmd.ExecuteNonQuery();
MessageBox.Show("Record added");

所以它将是

  mBuilder.setAutoCancel(true);