Wednesday, 15 April 2015

Why doesn't this broadcast receiver work in Android Lollipop? -


I have been declared this broadcast receiver in the manifest:

   & Lt; Action Android: name = "android.intent.action.BOOT_COMPLETED" /> & Lt; / Intent-Filter & gt; & Lt; / Receiver & gt;  

My receiver class:

  The public class VoiceLaunchReceiver extends android.content.broadcastReceiver {@Override Public Zero onReceive (Context ctx, Intent Intent) {Intent Service = New intent (CTX, Voice lawnsci class); // service.putExtra (action, true); Logs. I ("Josers", "Incoming Voice Launch Broadcast ..."); If (intent.getAction (.) Equals (Intent.ACTION_USER_PRESENT)) {Log.e ("joshcsr", "***********") \ nCSCS resumes (BC **** ** ****** "); Ctx.startService (service); } If (intent.getAction (.) Equals (Intent.ACTION_SCREEN_OFF)) {Log.e ("joshcsr", "********** \ nCSR screen (paused by BC) \ N *** ********* "); Ctx.stopService (service); }}}  

I registered SCREEN_OFF intent in the service onCreate:

  @Override Public Zero OnCreate () {super.onCreate (); // Register Screen of Broadcast Intent filter MIentant filter = new intent filter (intestine action_serenienaf); Register receiver (speechbrowse receiver, mentent filter); }  

Whenever the user rotates and turns the blocks on the screen, then it still has its own thing. It works perfectly in Jelly Bean 4.3 and lowers: My logs are shown within my broadcast receiver, unlocked when the screen is locked. Why will not this exact code work in Lollipop? Should I hear instead of new entries?

(I already know that systems send this intent, what I want to do is find it with my receiver)

I had the same problem and I fixed it (tested on 4.3 and 5.1). I was able to declare "android.intent.action.USER_PRESENT" in the manifest, as long as you have READ_PHONE_STATE permission, it's OK !! My mini app contains a broadcast receiver that responds to the screen in an on / off position, and runs a background service that recognizes the constant voice. If the screen is off, the validation is off here is the code, enjoy: Manifest:

   & Lt; Receiver Android: name = "classes.VoiceLaunchReceiver" & gt; & Lt; Intent-Filter & gt; & Lt; Action Android: name = "android.intent.action.USER_PRESENT" /> & Lt; Action Android: name = "android.intent.action.BOOT_COMPLETED" /> & Lt; Category android: name = "android.intent.category.DEFAULT" /> & Lt; / Intent-Filter & gt; & Lt; / Receiver & gt;  

BROADCAST RECEIVER: Public class VoiceLaunchReceiver extends BroadcastReceiver {@Override Public Zero onReceive (Context ctx, Intent Intent) {Intent Service = New Intent (ctx , Voice-LaunchSwirv.class); // service.putExtra (action, true); Logs. I ("Josers", "Incoming Voice Launch Broadcast ..."); If (intent.getAction (.) Equals (Intent.ACTION_USER_PRESENT)) {Log.i ("joshcsr", "*********** \ nCSR resume (BC) \ N *** ** ******* "); Ctx.startService (service); } If (intent.getAction (.) Equals (Intent.ACTION_SCREEN_OFF)) {Log.i ("joshcsr", "***********" \ nCSR stopped by screen (BC) \ N ** ********** "); Ctx.stopService (service); }}}

You can imagine, my USER_PRESENT broadcast receiver is no longer registered. I register at ACTION_SCREEN_OFF and the current My service form method, which was started by my receiver.

  @ Override Creates on Public Zero (super.onCreate (); // register screen on / off Broadcast Launcher = new voicemail receiver (); IntentFilter I = New Intent Filter (Intent.ACTION_SCREEN_OFF); I.addAction (Intent.ACTION_SCREEN_ON); RegisterReceiver (Launcher, I); Log.d ("joshcsr", "VoiceLaunch made service"); }  

In the end I turned on / off OnDestroy screen () Registering my service:

  @Override Public Zero OnDestroy () {super.onDestroy (); UnregisterReceiver (Launcher);}  

No comments:

Post a Comment