Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!