1

I used solution from there: Android - detect phone unlock event, not screen on

So, my activity onCreate:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    registerReceiver(
       new PhoneUnlockedReceiver(), new IntentFilter("android.intent.action.USER_PRESENT")
    );
}

And my receiver class:

public class PhoneUnlockedReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        KeyguardManager keyguardManager = 
            (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
        if (keyguardManager.isKeyguardSecure())
        {
            Toast.makeText(context, "Screen unlocked", Toast.LENGTH_LONG).show();
        }
    }
}

But it's not working, my onReceive method is never called. Any ideas what's wrong?

My Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.michal.popupmenu"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <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>
    </application>

</manifest>

As far as I know, there is no need to add anything to manifest if I choose to use registerReceiver, right?

Community
  • 1
  • 1
user1209216
  • 7,404
  • 12
  • 60
  • 123
  • post your Manifest.xml here – Nikhil Sep 09 '16 at 08:41
  • Possible duplicate of [A way to get unlock event in android?](http://stackoverflow.com/questions/20224637/a-way-to-get-unlock-event-in-android) – Jitesh Prajapati Sep 09 '16 at 08:42
  • Does it work if you register in the manifest instead? – Tim Sep 09 '16 at 08:45
  • 1
    Don't do this `registerReceiver(new PhoneUnlockedReceiver(), ..)`, you will be unable to unregister it because you have no reference to the receiver and have a leak – Tim Sep 09 '16 at 08:46

2 Answers2

1

As far as I know, there is no need to add anything to manifest if I choose to use registerReceiver, right?

Wrong. The advantage of an registered receiver in the manifest is the fact that it does not require your app to be running when the Intent is fired.

So your app probably is not active when the user unlocks the screen so there is no registerReceiver() called and therefore your receiver does not react.

Add the receiver in your manifest and it will work.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
0

Sorry, but could anyone explain the function: keyguardManager.isKeyguardSecure() always return false when I don't set PIN/pattern/password, return true when setting PIN/pattern/password although the screen is lock/unlock. So how could the code above running:

if (keyguardManager.isKeyguardSecure())
        {
            Toast.makeText(context, "Screen unlocked", Toast.LENGTH_LONG).show();
        }

Thanks.

RoShan Shan
  • 2,924
  • 1
  • 18
  • 38