Friday, 28 August 2015

android BroadcastReceiver (how to make BroadcastReceiver always running, even without opening the app)

1. you can create a receiver by two ways:

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml.

2. but only when you put it into your manifest, then it can run in the background without the app running. ( But after the app installed, you must let the user manually click and open the app once. at lease once)

If your receiver is registered in your manifest, it should always work. Even if your app is force closed.

This is an additional security feature as the user can be sure that only the applications he started will receive broadcast intents.


This does not mean the user has to start the application again after a reboot. The Android system remembers that the user already started it. Only one start is required without a forced stop by the user.

3. You can't create an app just has one receiver only. You app must at lease has one activity

Starting with Honeycomb, BroadcastReceivers are installed in a stopped state and will not trigger until an application has actually run, i.e. you will need an activity to have run at least once. This is explained in depth in this Commonsware blog post:

4. you can call system.out.println() to print log in the receiver. (but sometime doesn't work if your app is running on the background.)

No comments:

Post a comment