【发布时间】:2012-01-06 23:50:10
【问题描述】:
如何从 BroadcastReceiver 触发通知(不能使用大多数方法,也不能使用“this”)? 我需要它来使用我已经做过的数据库中的信息打开一个活动,但现在这些方法必须不起作用,我不能使用“this”
【问题讨论】:
标签: java android methods broadcastreceiver
如何从 BroadcastReceiver 触发通知(不能使用大多数方法,也不能使用“this”)? 我需要它来使用我已经做过的数据库中的信息打开一个活动,但现在这些方法必须不起作用,我不能使用“this”
【问题讨论】:
标签: java android methods broadcastreceiver
在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);
}
Activity 和 Service 派生自 Context。这就是为什么在上下文的许多(或所有)实例方法中,您可以使用this。如果是这种情况,那么您可以使用在onReceive 中收到的Context。
【讨论】: