简体   繁体   English

检测屏幕解锁

[英]Detect screen unlock

I used solution from there: Android - detect phone unlock event, not screen on我使用了那里的解决方案: Android - 检测手机解锁事件,而不是屏幕开启

So, my activity onCreate:所以,我的活动 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.但它不起作用,我的onReceive方法从未被调用。 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?据我所知,如果我选择使用 registerReceiver 就不需要在 manifest 中添加任何东西,对吧?

As far as I know, there is no need to add anything to manifest if I choose to use registerReceiver, right?据我所知,如果我选择使用 registerReceiver 就不需要在 manifest 中添加任何东西,对吧?

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.在清单中注册接收器的优势在于,当 Intent 被触发时,它不需要您的应用程序运行。

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.因此,当用户解锁屏幕时,您的应用程序可能未处于活动状态,因此没有调用registerReceiver() ,因此您的接收器没有反应。

Add the receiver in your manifest and it will work.在您的清单中添加接收器,它将起作用。

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.抱歉,但是谁能解释一下这个函数: keyguardManager.isKeyguardSecure()在我没有设置 PIN/pattern/password 时总是返回 false,在设置 PIN/pattern/password 时返回 true,尽管屏幕是锁定/解锁的。 So how could the code above running:那么上面的代码如何运行:

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

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM