Android - 为什么点击通知后Activity会打开?

发布于 2024-12-02 12:00:43 字数 991 浏览 0 评论 0原文

我有一个显示通知的功能,我从各种活动中调用该通知。

public static void CrearNotificacion(Context pContexto, String pTituloBarra, String pTitulo, String pTexto){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) pContexto.getSystemService(ns);

    Notification notification = new Notification(R.drawable.icono, pTituloBarra, System.currentTimeMillis());
    notification.defaults |= Notification.DEFAULT_SOUND;

    Intent notificationIntent = new Intent(pContexto, pContexto.getClass());
    PendingIntent contentIntent = PendingIntent.getActivity(pContexto, 0, notificationIntent, 0);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.setLatestEventInfo(pContexto, pTitulo, pTexto, contentIntent);
    mNotificationManager.notify(1, notification);
}

工作完美,问题是按下通知会打开创建通知的活动,这是错误的,我认为当我选择通知时,notiifcacion 活动不应该打开。

为什么?有什么办法可以解决这个问题吗?

当我选择通知时,我不想打开任何活动。

从现在开始谢谢。

I have a function to display a notification, which I call from various activities.

public static void CrearNotificacion(Context pContexto, String pTituloBarra, String pTitulo, String pTexto){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) pContexto.getSystemService(ns);

    Notification notification = new Notification(R.drawable.icono, pTituloBarra, System.currentTimeMillis());
    notification.defaults |= Notification.DEFAULT_SOUND;

    Intent notificationIntent = new Intent(pContexto, pContexto.getClass());
    PendingIntent contentIntent = PendingIntent.getActivity(pContexto, 0, notificationIntent, 0);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.setLatestEventInfo(pContexto, pTitulo, pTexto, contentIntent);
    mNotificationManager.notify(1, notification);
}

works perfect, the problem is that pressing on the notification opens the activity that created the notification and that's wrong, I think the notiifcacion activity should not open when I select the notification.

Why? there any way to fix this?

I do not want to open any activity when I select the notification.

thanks from now.

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

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

发布评论

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

评论(2

紫竹語嫣☆ 2024-12-09 12:00:43

为了在点击通知时不执行任何操作,您可以设置一个空的 Intent,如下所示:

Intent notificationIntent = new Intent() ;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;

In order to have no action taken when clicking the notification, you may set an empty Intent as follows:

Intent notificationIntent = new Intent() ;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
空城之時有危險 2024-12-09 12:00:43
Intent notificationIntent = new Intent(pContexto, pContexto.getClass());

我想这是你必须改变的行

Intent notificationIntent = new Intent(pContexto, yourClass.class);
Intent notificationIntent = new Intent(pContexto, pContexto.getClass());

i guess this is the line you have to change with

Intent notificationIntent = new Intent(pContexto, yourClass.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文