我在那里使用了解决方案:Android - detect phone unlock event, not screen on
所以,我的活动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")
);
}我的接受者课:
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();
}
}
}但是它不起作用,我的onReceive方法从未被调用过。有什么好主意吗?
我的安卓宣言:
<?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>据我所知,如果我选择使用registerReceiver,就没有必要添加任何要显示的内容,对吗?
发布于 2016-09-09 08:59:33
据我所知,如果我选择使用registerReceiver,就没有必要添加任何要显示的内容,对吗?
不对。清单中注册接收方的优点是,它不要求在启动意图时运行应用程序。
因此,当用户解锁屏幕时,您的应用程序可能没有活动,因此没有调用registerReceiver(),因此您的接收方不会做出任何反应。
将接收者添加到您的清单中,它将工作。
发布于 2016-09-09 09:26:36
对不起,谁能解释一下这个函数:当我不设置PIN/pattern/密码时,keyguardManager.isKeyguardSecure()总是返回false;在设置PIN/pattern/password时返回true,尽管屏幕是锁/解锁的。那么上面的代码如何运行:
if (keyguardManager.isKeyguardSecure())
{
Toast.makeText(context, "Screen unlocked", Toast.LENGTH_LONG).show();
}谢谢。
https://stackoverflow.com/questions/39407265
复制相似问题