Greetings! I'm trying to register an unlock event on the device screen. I hang the receiver with action = "android.intent.action.USER_PRESENT" .

When the application is active, everything works. When I throw the application out of the curtains of the recently launched, it does not work. And this problem is observed with API21 (on 4.4. - everything works). It seems as expected, Google with 21 api is very seriously engaged in optimizing the work of background applications, but somehow this receiver should work?

Manifest code:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.appdroid.develop.receiverscreenlock"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ScreenReceiver"> <intent-filter android:priority="2147483647"> <action android:name="android.intent.action.USER_PRESENT" /> </intent-filter> </receiver> </application> </manifest> 

ScreenReceiver class ScreenReceiver :

 public class ScreenReceiver extends BroadcastReceiver { private static final String TAG = "myLog"; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG,"onReceive "+ intent.getAction()); Toast.makeText(context,"unlock screen",Toast.LENGTH_SHORT).show(); } } 
  • This receiver is on the list of those that do not work from the manifest. It needs to be created in the activation code. Although not the fact that the problems will end on this - Denis Isychenko
  • You are not right. This receiver works without problems from the manifest. The problem with the failure of the receiver is that the android (as I wrote above) since version 5.0 has seriously started optimizing battery life. This optimization and kills the receiver. - Sergey K.

0