Can we Only work with one broadcast receiver? - android

Can We only work with one broadcast receiver ?
I have some broadcast receiver and they works well , :
Note: Only one BroadcastReceiver class can be specified per application. Should you need to incorporate two or more BroadcastReceivers from different SDKs, you will need to create your own BroadcastReceiver class that will receive all broadcasts and call the appropriate BroadcastReceivers for each type of Broadcast.

Yes, you may use a single BroadcastReceiver to catch all action strings. Make sure you do add all the action string in your IntentFilter used by that receiver to make it work.

An <application> can contain multiple <receiver> and each <receiver> can contain multiple <intent-filter>. E.g.:
<application>
<receiver android:name="ReceiverA">
<intent-filter>
<action android:name="android.intent.action.ACTION1"/>
</intent-filter>
</receiver>
<receiver android:name="ReceiverB">
<intent-filter>
<action android:name="android.intent.action.ACTION2" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION3" />
<data android:scheme="file" />
</intent-filter>
</receiver>
</application>
But you can only have one "com.google.android.apps.analytics.AnalyticsReceiver" - that is IMO what the documentation means.

Related

Android TV (Emulator Preview, level L) - BroadcastReceiver does not work from manifest

I rely on broadcast send/receive for my app to work.
This code works perfectly on all platforms, but on latest Android Preview L, the broadcast is not received:
Intent intent = new Intent("com.my.BROADCAST_RECEIVED");
sendBroadcast(intent);
Receiver is registered in the manifest, as usual:
<receiver
android:name=".SimpleBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.my.BROADCAST_RECEIVED" />
</intent-filter>
</receiver>
Note: if the receiver is registered in runtime (i.e. via registerReceiver(..)) - it does receive the broadcast.
Any information about this?
Found a different answer related to not receiving boot complete on SmartTv .
So as an act of despair I decided to give it a try and it worked!
Add a category tag to the intent filter. It's not documented anywhere:
<receiver
android:name=".SimpleBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.my.BROADCAST_RECEIVED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Hope this helps someone.

Registering a Phonestate receiver after rebooting

I have implemented a receiver like this (in the manifest file)
<receiver android:name="com.phonelight.realparrot.RecorderBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE">
</action>
</intent-filter>
</receiver>
If the state of the phone changes, the recorder broadcast receiver is invoked.
Everything is fine. However, If I reboot the device, the receiver is never invoked until I run my application.
I need to register (not invoking) this receiver after booting.
Many thank,
[Edit]
I solved the problem by adding the following receiver to the Manifest file
<receiver android:name="com.phonelight.realparrotpro.RecorderBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I did not register the RecorderBroadcastReceiver in the java code though.
I only added the above receiver. It means invoking anything from an app will register all the receivers written in the Manifest file.
You need to create a receiver for onBootComplete and then register your receiver there. This way your receiver will get registered even after reboot.
<receiver android:name="App_Receiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

I can't receive broadcast on battery state change?

I am having the exact same problem as this post: Battery broadcast receiver doesn't work. But it seems no one has answered that question.
Here is my BroadcastReceiver:
public class BatteryLevelReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.v("plugg", "plug change fired");
Toast.makeText(context, " plug change fired", Toast.LENGTH_LONG).show();
}
And here is my AndroidManifest.xml:
<receiver android:name=".ReceversAndServices.BatteryLevelReceiver">
<intent-filter android:priority="900">
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>
<receiver android:name=".ReceversAndServices.BatteryLevelReceiver">
<intent-filter android:priority="900">
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
I have also added this line to the manifest:
<uses-permission android:name="android.permission.BATTERY_STATS"/>
But still no success!
I would really appreciate if someone could advise me what I am doing wrong.
From the documentation for ACTION_BATTERY_CHANGED:
You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED, and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers.
There you have it: you must explicitly register for it from your Java code.
I just followed the Android Developer Guide's on Monitoring the Battery Level and Charging State and had immediate success. If BatteryLevelReceiver is it's own class then I would recommend:
<receiver android:name=".BatteryLevelReceiver">
<intent-filter android:priority="900">
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
Addition
I'm willing to guess that you wrote BatteryLevelReceiver as a nested class in ReceversAndServices. According to Receiver as inner class in Android, you cannot do that with non-static classes. You could make BatteryLevelReceiver a static class and register the receiver in onResume(), but then your app will need to be running to catch the events... Move your receiver to a separate class and register these Intents:
<receiver android:name=".BatteryLevelReceiver">
<intent-filter android:priority="900">
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.intent.action.BATTERY_OKAY" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
(Not BATTERY_CHANGED as Darshan Computing pointed out.)
Make sure you have the battery receiver class not as a subclass of another one but as a seperate class in your project.
Also try using Context.registerReceiver() method explicitly in your code and do not forget to unregister it: http://developer.android.com/reference/android/content/Context.html#registerReceiver%28android.content.BroadcastReceiver,%20android.content.IntentFilter%29

Android broadcastreceiver static registering problems

I understand that I can register a broadcast receiver either statically through the manifest or programmatically by registering and unregistering the receiver in an activity. In my case I want the OS to instantiate and call my broadcast receiver when my code is not in memory. So I included my receiver in my manifest. Unfortunately if my code is not in memory, my broadcast receiver never runs. I am very confused as to why this is. I have included snippets of my manifest below. Please let me know if there is something wrong/missing with this. Thank you.
<receiver android:name=".DesktopConnectionReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="android.intent.action.UMS_CONNECTED"/>
</intent-filter>
</receiver>

How do I add another intent and how do I call this receiver in code?

I have following receiver that listens to Boot_Completed
<receiver android:name=".receivers.ActionBootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
I want to add another intent-filter with my own custom action. And make it private to my app if possible. This is mostly for code reuse so I can run same code path as when BOOT_COMPLETED.
So, I need following (if it's even possible)
1. intent-filter and make it private to my app
2. Code to send that intent so my receiver get's it.
Thanks!
Just use another <action> tag! It is all you need to register receiver for an particular intent.

Categories

Resources