【问题标题】:how to fire notification from BroadcastReceiver?如何从 BroadcastReceiver 触发通知?
【发布时间】:2012-01-06 23:50:10
【问题描述】:

如何从 BroadcastReceiver 触发通知(不能使用大多数方法,也不能使用“this”)? 我需要它来使用我已经做过的数据库中的信息打开一个活动,但现在这些方法必须不起作用,我不能使用“this”

【问题讨论】:

    标签: java android methods broadcastreceiver


    【解决方案1】:

    onReceive 方法中,您会得到一个Context 对象。所以用它来获取NotificationManager 并触发你的通知。

    public void onReceive(Context ctx, Intent intent) {
        NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        //Create the notification here.
        nm.notify(NOTIFICATION_ID, notification);
    }
    

    ActivityService 派生自 Context。这就是为什么在上下文的许多(或所有)实例方法中,您可以使用this。如果是这种情况,那么您可以使用在onReceive 中收到的Context

    【讨论】:

    • 对于我的用例,如果有数百个用于触发警报的pendingIntent,需要在BroadcastReceiver 收到BOOT_COMPLETED 操作后重置怎么办?由于有很多警报,我将如何在后台重置这些警报?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2020-05-11
    相关资源
    最近更新 更多