Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the intent action USER_PRESENT?

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!

like image 437
saman0suke Avatar asked Feb 25 '14 23:02

saman0suke


People also ask

What is Intent in android with example?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.

What is an Intent object in android?

An Intent object is a bundle of information which is used by the component that receives the intent as well as information used by the Android system. An Intent object can contain the following components based on what it is communicating or going to perform −


2 Answers

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

like image 53
shadygoneinsane Avatar answered Nov 03 '22 01:11

shadygoneinsane


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

like image 29
OneWorld Avatar answered Nov 03 '22 01:11

OneWorld