0

How do I make a broadcast receiver which runs always, from the very start of the device? Is this possible without starting the application where it is declared?

If not I guess I would have to start my application when the device starts. But this probably adds to much overhead and it's unwanted.

I want this always runnning broadcast receiver in order to listen always for c2dm notifications. These should notify the user that there are new messages in the application.

User
  • 31,811
  • 40
  • 131
  • 232
  • why you are not creating it when your application start... – Bharat Sharma May 03 '12 at 14:13
  • 2
    Add action to your receiver as – Pankaj Kumar May 03 '12 at 14:14
  • 1
    If you read [the C2DM documentation](https://developers.google.com/android/c2dm/), you will see that this is already covered, by using the technique in onelise14's answer. – CommonsWare May 03 '12 at 15:17
  • This is no longer true as of 3.1, newly installed applications that have not yet run are considered to be in the "stopped" state and will not receive Broadcast intents until the application has been run at least once by being explicitly started by the user. – Maks Jul 10 '12 at 23:54

2 Answers2

1

If you add the BroadcastReceiver to your Manifest with an Intent Filter to listen for a specific intent, the Receiver will be active upon install.

SeanPONeil
  • 3,901
  • 4
  • 29
  • 42
  • This is an old post but it is important to highlight that it doesn't work anymore with Android 8 and above. See https://developer.android.com/guide/components/broadcasts.html#receiving_broadcasts "Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers. If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that do not target your app specifically)." – OlivierGrenoble Sep 19 '19 at 13:42
0

what do you mean "run always" ?

if you need something to be alive for a long time , and from the OS bootup , you need to :

  1. let the app to be installed only on the internal storage (otherwise it won't work).

  2. set the broadcast receiver to listen to the boot intent .

  3. upon receiving the boot intent, start a service and there listen to the intent you wish to listen to.

android developer
  • 114,585
  • 152
  • 739
  • 1,270