Search code examples
androidbroadcastreceiverandroid-notifications

BroadcastReceiver notification and activity messages


I'm very new to Android and I need a little assistance here.

I have an activity and a broadcast receiver. Broadcast receiver should listen SMS Intents and:

  1. if Activity is visible and interacting - SMS should appear in activity
  2. if no - broadcast receiver should create notification with SMS, so if user click on that notification, the activity will be shown

So, i read a lot of articles about broadcasts and i can implement one of these variant(1 - as a inner broadcastreceiver class and 2 - as global broadcast receiver declared in manifest) , but how i can have both? Could you please propose some idea?


Solution

  • Ok, so in that case i post my own solution.

    I declare two broadcast receiver: one, declared in Manifest, and doing post to tray notification, and second - declared as an inner class of main activity and doing post to activity components. Also i have subclass of Application class to track main activity state (i.e. visible or not). And so global broadcast receiver post any notifications only if it see that activity is hide right now.

    I think its reasonably solution.