last day (14 days later) » 

6:06 AM
0
Q: Android Class in MainActivity does not receive Broadcast from Alarm Manager

zehataI am trying to use Alarm Manager to send and receive a broadcast that will call a method to create a notification. I want the notification to be created even when app is closed. I created a class with this method that creates the alarm in Alarm Manager: @android.webkit.JavascriptInterface public...

 
Any nested class used for an app component - like your loadqotd class - must be public static. I would also suggest following Java naming convention, and capitalize your class names.
 
@MikeM. It seems that I get the error non-static method getSystemService(String) can not be referenced from static context
 
Call it on the Context passed into onReceive(). That is, NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);. You could just make it a separate class file, btw, instead of a static nested class.
 
@Amir it didn't solve the problem anyway I already have the broadcast in alarmmanager I just didnt receive it
@MikeM. OK let me try
 
You sure your manifest is correct? We can't tell from that pared-down snippet. Also, I assume you're certain the alarm is firing when you expect, yeah?
 
6:06 AM
Yah, no error when building. Its nested in application in manifest
 
Well, that doesn't necessarily mean it's correct. It's possible to have errors in the manifest that will build just fine.
 
Shall I move this to chat?
 
Sure. I got nothin' doin'.
I'm on a phone, though, so I might not respond very quickly.
 
I tried it in a new class file but it seems notification_service cant be found
context.notification worked though
 
I had edited that comment. Check it again. context.getSystemService(Context.NOTIFICATION_SERVICE).
 
6:12 AM
I still don't get the notification
btw i changed manifest to .loadqotd instead of mainactivity alrdy
 
Are you sure the alarm is firing when you expect?
 
as in?
it is calling fine into alarm manager as I could see from a toast that tells me whether broadcast succeded
 
I mean, are you sure you have the correct time, the time you expect, correctly set on the Calendar.
 
yes. I am quite sure
 
Oh, so it's just the Notification?
 
6:18 AM
ya quite sure
RTC_WAKEUP #5: Alarm{b76cbfd type 0 when 1471760280264 ethical.hackers.lighthouse}

      tag=*walarm*:ethical.hackers.lighthouse/.loadqotd

      type=0 whenElapsed=+23h57m4s284ms when=2016-08-21 14:18:00

      window=+18h0m0s0ms repeatInterval=86400000 count=0 flags=0x0

      operation=PendingIntent{66a20f2: PendingIntentRecord{e65f343 ethical.hackers.lighthouse startService}}
This is the alarm manager broadcast
 
Right, if you're getting the Toast, then, yeah, it's firing. Trying to think why that Notification wouldn't work, though.
Does that Notification code work if you just call it directly in your Activity?
 
ok let me try
i think ive just pinpointed the problem due to the wrong date settings. Thank you!
 
Wait, what, now? How was onReceive() firing the Toast if the time was wrong?
 
i just checked it wasnt due to the date
probably because i dumped it too late so it went till tomorrow since its set to repeat
 
OK. Check the Notification directly to make sure it at least works normally.
Yeah, I just tested it, subbing my MainActivity, and it's good. Should work the same in a Receiver.
 
6:33 AM
Notification works fine
 
So, it's just not working in the Receiver?
Hmm...
Have you cleaned and rebuilt lately? And try a complete uninstall/reinstall.
Of the app, I mean, not your IDE. :-)
 
ok let met try
 
Well, I just tested everything, and it works as expected for me.
Are you testing on a device, or an emulator?
 
device
 
6:49 AM
What kind? I know some OEMs have extra, funky permissions settings that disallow apps to do certain things from the background. Have you checked for that?
 
a moto e running on cyanogenmod
 
Maybe it's the ROM. Does it have any extra stuff in app settings that might be restricting your app's behavior?
 
one thing though, why is it that my broadcast is a start service instead of a broadcast intent
 
Aw, jeez. Are you talking about PendingIntent.getService(). Yeah, that should be PendingIntent.getBroadcast(). Can't believe I didn't notice that.
I didn't think to check 'cause you said you were getting the Toast. How were you ever getting the "received" Toast?
 
i guess perhaps it was due to a test code in oncreate?
so it should be getbroadcast instead on get service?
oh and yes it was just fixed
thank you so much for all the trouble
 
7:00 AM
Phew! Cool. No problem. Glad to help.
I'll put together an answer with all the changes we made, here in a minute.
Just as a side note, you can put that Receiver back into your `MainActivity`, if you really want. Up to you. I do that sometimes, just for code organization, to keep things tidy, etc. Just be sure to make it `static` if you do.
Cheers!
 
sure
 

last day (14 days later) »