how to fire notification from BroadcastReceiver?

☆樱花仙子☆ 提交于 2019-12-30 16:26:44

问题


how to fire notification from BroadcastReceiver (can't use most methods and can't use "this")? I need it to open a activity with info from the DB I already did it but now must of the methods dosen't work and I cant use "this"


回答1:


In the onReceive method you get a Context object. So use it to get the NotificationManager and fire your notification.

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

An Activity and a Service are derived from Context. That's why, in many (or all) of the instance methods of a context, you can use this. If that's your case, then you can use the Context you receive in onReceive.



来源:https://stackoverflow.com/questions/8280275/how-to-fire-notification-from-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!