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.
http://developer.android.com/intl/zh-cn/reference/android/content/BroadcastReceiver.html#ReceiverLifecycle



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.

http://stackoverflow.com/questions/10707354/broadcastreceiver-when-application-is-not-running





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

Tip


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.
http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html


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:

http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
http://developer.android.com/intl/zh-cn/about/versions/android-3.1.html#launchcontrols
http://stackoverflow.com/questions/12315325/can-android-application-have-only-broadcast-recevier-and-service-without-activit


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