This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Android and the fly likes Problem with Notifications when calling them from a Non-Activity class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Problem with Notifications when calling them from a Non-Activity class" Watch "Problem with Notifications when calling them from a Non-Activity class" New topic
Author

Problem with Notifications when calling them from a Non-Activity class

Sean Michael Hayes
Ranch Hand

Joined: Feb 08, 2012
Posts: 54

I'm running a service on my Android Application to listen for SNMP trap messages (calling my trap receiver's listen() method) on a port. When a PDU is recieved on that port, a Notification is meant to be sent to my phones status bar.

However, when I try sending a test trap that will automatically be picked up by my receiver class, it gets the PDU but no Notification is sent. I'm getting a NullPointerException on this line:



The line was originally:



The first line was an attempt at solving the problem as I thought the issue was it was not getting an Activity Context to call NOTIFICATION_SERVICE so I found this solution where the current Activity on top of the stack is stored in an Activity variable and we can call it using the ref_currentActivity() method and get the Context from that but it doesn't seem to work. Here's the code for my trapReceiver class:



BTW I'm using API level 8
Sangel Kapoor
Ranch Hand

Joined: Jan 12, 2013
Posts: 51

Hii Sean

Use Context object available in onReceive() method.

context.getSystemService();


NOTE: Both Activity and Service extends Context , so if you are launching Notification from Service/Activity , you can simply use this.
Sean Michael Hayes
Ranch Hand

Joined: Feb 08, 2012
Posts: 54

Sahil Rally wrote:Hii Sean

Use Context object available in onReceive() method.

context.getSystemService();


NOTE: Both Activity and Service extends Context , so if you are launching Notification from Service/Activity , you can simply use this.


Thank you for the reply Sahil, however I tried your technique and I was still getting a NullPointerException. I eventually solved the problem by simply passing in the context of my service into the reciever class and assigning it to a variable there.
 
jQuery in Action, 2nd edition
 
subject: Problem with Notifications when calling them from a Non-Activity class
 
Similar Threads
Chat window Issues in android
How to start new activity when click on a listview and pause the process that is being in background
Android CustomListView getView() getting nullpointerexception
android custom list view position and click event problem(list getting dynamic data from server)
How to add new country and flag in the list view