BroadcastReceiver is not called when the screen is locked on Android

In my application, when a notification arrives, BroadcastReceiver not called if the screen is locked. But when the screen is unlocked, BroadcastReceiver is called and displays a notification.

I also added the following permission to my manifest:

 android.permission.WAKE_LOCK 

But still not working.

+4
source share
2 answers

Here is the code that works for me:

 NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(...); ... mManager.notify(0, notification); PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); wl.acquire(15000); 

Make sure you send a notification from your server using delay_while_idle=0 (this is the default) or the notification will not be sent by GCM until your device wakes up.

+15
source

Open the action A that you want to start with onReceive (....) BroadCastReceiver. Insert this into onCreate () Activity A

  final Window win= getWindow(); win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 

Make sure you do not embed it before setContentView (....) :-)

0
source

Source: https://habr.com/ru/post/1499313/


All Articles