如何向通知(状态栏通知)添加标志?

发布于 2024-09-27 20:47:21 字数 189 浏览 1 评论 0原文

我正在遵循开发人员指南中的教程,但我遇到了问题...

教程说:“要在用户从通知窗口中选择状态栏通知时清除状态栏通知,请将“FLAG_AUTO_CANCEL”标志添加到您的通知对象中”

但是...如何将标志添加到我的通知中?

notification 没有任何类型的功能来添加标志......那么?我怎样才能做到呢?

im following the tutorial from developers guide but i have a problem...

tutorial says: "To clear the status bar notification when the user selects it from the Notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification object"

but... how i can add the flag to my notification?

notification doesn't have any kind of function to add flags.... then? how i can do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云巢 2024-10-04 20:47:21

通知标志是公共成员字段。

Notification notif = new Notification(R.drawable.icon, shortMsg,
                System.currentTimeMillis());
notif.flags |= Notification.FLAG_AUTO_CANCEL;

Notification flags is a public member field.

Notification notif = new Notification(R.drawable.icon, shortMsg,
                System.currentTimeMillis());
notif.flags |= Notification.FLAG_AUTO_CANCEL;
攀登最高峰 2024-10-04 20:47:21

当您定义 Intent 时,标志会出现在末尾,如下所示:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, Notification.FLAG_AUTO_CANCEL);

Flags go on the end when you define the Intent, like this:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, Notification.FLAG_AUTO_CANCEL);
决绝 2024-10-04 20:47:21

下面是您可以尝试的代码,它的工作原理。

public void notification(Context context,String title, String text) {
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder =
        new NotificationCompat.Builder(context)
        .setAutoCancel(true)
        .setSound(alarmSound)
        .setSmallIcon(R.drawable.appicon)
        .setContentTitle(title)
        .setContentText(text);

    Intent notificationIntent = new Intent(this,gps_get_data.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,                                           PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
}

Below is the code you can try, Its working.

public void notification(Context context,String title, String text) {
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder =
        new NotificationCompat.Builder(context)
        .setAutoCancel(true)
        .setSound(alarmSound)
        .setSmallIcon(R.drawable.appicon)
        .setContentTitle(title)
        .setContentText(text);

    Intent notificationIntent = new Intent(this,gps_get_data.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,                                           PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文