16

I have a clock widget application, and I need to recognize when the phone has been unlocked or not, I believe I can use action USER_PRESENT for that, but I can't get it to launch in the BroadcastReceiver class, I set it in the manifest like this:

    <receiver
        android:name="com.myApp.myApp.MyWidgetIntentReceiver"
        android:exported="false"
        android:label="widgetBroadcastReceiver" >
        <intent-filter> 
            <action android:name="android.intent.action.USER_PRESENT" >
            </action>                               
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/demo_widget_provider" />
    </receiver>

And this is how I trying to get it in the BroadcastReceiver:

public class MyWidgetIntentReceiver extends BroadcastReceiver{

    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_USER_PRESENT){
            Log.i("TICK", intent.getAction());          
        }
    }

}

It's not firing after I unlock the phone, can you help me out or provide me a better way to check when the phone has been unlocked? thanks!

saman0suke
  • 762
  • 1
  • 8
  • 24
  • The below stack flow link should probably help you: http://stackoverflow.com/questions/3446202/android-detect-phone-unlock-event-not-screen-on – Sushil Feb 25 '14 at 23:42
  • I've tried your code, and found it works well. It can receive ACTION_USER_PRESENT when the app is activated. But when someone kill your process in background, it takes 1 or more seconds to restart your process for broadcast. And in this interval, it can't receive ACTION_USER_PRESENT. Hope it helps. – Euporie Feb 26 '14 at 02:57
  • Could you post the whole code? I've seen some examples where they use ***registerReceiver*** and attach an IntentFilter to it, but in my code, I just define the filters in the manifest, where I have like 4 actions, all of them works, but they are defined by me, not system actions, with this one I cannot get it to work. – saman0suke Feb 26 '14 at 13:58

3 Answers3

7

Remove android:exported="false"

android:exported:

Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.

Source : developer.android.com

shadygoneinsane
  • 2,226
  • 1
  • 24
  • 47
5

Remove android:exported="false". That worked for me on Stock Android 5

OneWorld
  • 17,512
  • 21
  • 86
  • 136
2

I got it to work by using registerReceiver in the onUpdate method of the AppWidgetProvider class and passing an instance of the BroadcastReceiver class to register the Intent.ACTION_USER_PRESENT, since adding it only in the Manifest was not doing anything. Thank you!

saman0suke
  • 762
  • 1
  • 8
  • 24