2

我有一个可能会显示通知的服务,问题是一旦设置了通知,点击时它都不会清除,也不会在滑动时清除。我正在使用标志Notification.FLAG_AUTO_CANCEL,但它似乎没有做任何事情..

private NotificationManager nm;
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
private final int NOTIFY_ID = 1;
private void showNotification(String date, String name) {
        try{
            CharSequence text = "You have new Event";
            Notification notification = new Notification(R.drawable.small_icon, text, System.currentTimeMillis());
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, viewEvents.class).putExtra("date", date), 0);
            notification.setLatestEventInfo(this, name, "Date: "+date, contentIntent);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notification.defaults |= Notification.DEFAULT_LIGHTS;
            notification.defaults |= Notification.FLAG_AUTO_CANCEL;
            nm.notify(NOTIFY_ID, notification);
        }catch(Exception e){
            Log.i("Notification",e.toString());
        }
    }

那么我做错了什么?

4

3 回答 3

3

尝试使用Notification.Builderor而不是手动NotificationCompat.Builder滚动。Notification

除其他外,这将防止您的代码中出现错误,您正在申请FLAG_AUTO_CANCELdefaults不是flags.

于 2013-05-15T13:58:36.553 回答
0

Notification.FLAG_AUTO_CANCEL在您的代码中被注释掉。

但如果不是,则Notification.DEFAULT_LIGHTS应该在 notification.defaults 中设置标志,而不是在 notification.flags 中

于 2013-05-15T13:55:17.100 回答
0

我使用此代码显示/隐藏通知:

private Handler handler = new Handler();
private Runnable task = new Runnable() {
@Override
public void run() {
    NotificationManager manager = (NotificationManager) contesto.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent launchIntent = new Intent(contesto.getApplicationContext(), SearchHistory.class);
    PendingIntent contentIntent = PendingIntent.getActivity(contesto.getApplicationContext(), 0, launchIntent, 0);
    //Create notification with the time it was fired
    NotificationCompat.Builder builder = new NotificationCompat.Builder(contesto);
    builder.setSmallIcon(R.drawable.ic_launcher).setTicker("MESSAGE HERE!").setWhen(System.currentTimeMillis()).setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND).setContentTitle("TITLE").setContentText("MESSAGE").setContentIntent(contentIntent);
    Notification note = builder.build();
    manager.notify(100, note);
}
};

并调用 just put:

handler.post(task);
于 2013-05-15T14:32:56.607 回答