【问题标题】:Call activity when on notification tap event在通知点击事件时调用活动
【发布时间】:2010-09-08 09:19:46
【问题描述】:

我想在用户下拉通知并点击该通知时调用该活动。我该怎么做?

【问题讨论】:

    标签: android push-notification


    【解决方案1】:

    Notification 对象上调用setLatestEventInfo(),提供PendingIntent,当他们在通知抽屉中点击您的条目时将启动您的活动。这里是 a sample project 的演示。

    【讨论】:

    • 点击通知栏上的通知就可以开始活动了。但看起来它创建了新的活动,尽管当前活动正在运行。我试图在调用PendingIntent 时将标志传递给Intent,但看起来没有用。那么在单击通知时创建新活动之前如何关闭当前活动?
    • @HuyTower: setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) 你的Intent 应该可以工作。
    • @CommonsWare:非常适合我!
    【解决方案2】:

    假设notif 是您的Notification 对象:

    Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
    notif.contentIntent = contentIntent;
    

    【讨论】:

    • 点击通知栏上的通知就可以开始活动了。但看起来它创建了新的活动,尽管当前活动正在运行。我试图在调用PendingIntent 时将标志传递给Intent,但看起来没有用。那么在单击通知时创建新活动之前如何关闭当前活动?
    【解决方案3】:

    这是点击通知时调用活动的代码

    Notification notif = new Notification(R.drawable.ic_launcher,"List of Contacts...", System.currentTimeMillis());
    Intent notificationIntent = new Intent(context,AllContacts.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,            notificationIntent, 0);
    notif.setLatestEventInfo(context, from, message, contentIntent);
    nm.notify(1, notif);
    

    【讨论】:

    • 点击通知栏上的通知就可以开始活动了。但看起来它创建了新的活动,尽管当前活动正在运行。我试图在调用PendingIntent 时将标志传递给Intent,但看起来没有用。那么在单击通知时创建新活动之前如何关闭当前活动?
    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多