Android 判断手机是不是黑屏
Posted 小zhong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 判断手机是不是黑屏相关的知识,希望对你有一定的参考价值。
android没有提供专门的API来检测当前手机是否锁屏了。但是,在监听机制中,Android有3个广播与锁屏相关。
代码如下:
public class MainActivity extends AppCompatActivity {
public static final String TAG = "SCREEN";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_USER_PRESENT));
}
@Override
protected void onDestroy() {
unregisterReceiver(mBroadcastReceiver);
super.onDestroy();
}
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (!TextUtils.isEmpty(action)) {
switch (action) {
case Intent.ACTION_SCREEN_OFF:
Log.i(TAG, "屏幕关闭,变黑");
break;
case Intent.ACTION_SCREEN_ON:
Log.i(TAG, "屏幕开启,变亮");
break;
case Intent.ACTION_USER_PRESENT:
Log.(TAG, "解锁成功");
break;
default:
break;
}
}
}
};
}
以上是关于Android 判断手机是不是黑屏的主要内容,如果未能解决你的问题,请参考以下文章