Custom Broadcast receiver not getting invoked in Lollipop - android

I have declared a receiver in the Manifest, with an intent filter. It is getting invoked correctly in Jellybean but not getting invoked in Lollipop. Is the way a receiver is registered different for Lollipop, is there a need to register it programmatically ? Appreciate your help.
<receiver
android:name=".IPCallConnReceiver"
android:enabled="true"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.lib.ip.CALL" />
</intent-filter>
</receiver>

Related

Automatically detect if the system date has changed

I am trying to start a countdown timer whenever system date has changed or the user has changed the system date manually. I have used this broadcast action to detect if the system date has changed but it's not working. There are lot of threads on this issue. Some users are saying that it only works when user changes the date manually from settings and some users are saying that it works in both ways i.e date gets changed automatically or user changes it manually from settings. But in my case it's not working in both of the cases. Some users are suggesting to use Alarm Manager instead of this broadcast action i.e "android.intent.action.DATE_CHANGED".
Following is my implementation of the broadcast:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.byteshaft.a1440time">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receivers.OnDateChangeReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED"/>
</intent-filter>
</receiver>
</application>
</manifest>
My broadcast receiver class:
public class OnDateChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_DATE_CHANGED)){
System.out.println("Changed");
}
}
}
If Alarm Manager is the only solution then is there any guide on how to implement it in a proper way?
Do you want to get notified only if the time of the device is set ?
You can listen on the android.intent.action.TIME_SET broadcast instead.
It is triggered everytime the system time is modified.
If your app is targeting Android 8.0 (API level 26) or higher this broadcast cannot be registered in the AndroidManifest.xml , it can only be registered via Context.registerBroadcastReceiver
For detect changes of date you have to use two intent actions:
<receiver android:name=".BroadcastReceiverTest">
<intent-filter>
<action android:name="android.intent.action.TIME_SET"/>
<action android:name="android.intent.action.TIMEZONE_CHANGED"/>
</intent-filter>
</receiver>
I tested and it works.
During target the API level 26 or higher, you can not use "android.intent.action.DATE_CHANGED" for implicit broadcasts in manifest.

How to unregister MediaButtonReceiver onDestroy?

iam using MediaButtonReciever in my Streaming service to listen handle head sets and different devices action
i'm declaring it in Manifest like this
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
The actions works just fine as long as the app in the background
once the app is terminated if i clicked on any MediaButton it crashes as the media button with the following crash
Fatal Exception: java.lang.RuntimeException: Unable to start receiver android.support.v4.media.session.MediaButtonReceiver: java.lang.IllegalStateException: Could not find any Service that handles android.intent.action.MEDIA_BUTTON or a media browser service implementation
the problem is the receiver keeps receiving even if the app is destroyed, now how can i unregister the receiver once the app close ?
i have tried audioManager.unregister(MediaButtonReciever) but its depreciated
the problem was that i was using the default class in my manifest like this
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
which was wrong the the receiver automatically unregister after onReceive() is finished
Once you return from onReceive(), the BroadcastReceiver is no longer active, source
so all idid was i extened MediaRecieverButton in my custom class MyMediaButtonReceiver
and edited my manifest like this
<receiver android:name=".MyMediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
and it worked just fine
If you only want to handle buttons when your app is running, then you shouldn't declare this in the manifest. You should register and unregister the BroadcastReceiver dynamically in your code when you want it to respond to events.
To register the receiver, do:
BroadcastReceiver receiver = new MediaButtonReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
registerReceiver(receiver, filter);
When you are done (ie: in onDestroy() of your component), unregister the receiver like this:
unregisterReceiver(receiver);
in my case I got this error and I solve it by set enable flag of My service to true:
android:enabled="true"
<service
android:name=".MyService"
android:enabled="true"
android:multiprocess="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</service>

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>

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>

Categories

Resources