Android手机屏幕的三种状态

Android手机屏幕有三种状态变化分别是:屏幕开启变亮,屏幕解锁,屏幕关闭变黑。

这三种状态系统都会发出对应的广播,外部应用只需去注册对应的广播的来监听即可得到屏幕的状态来做一些操作。

private void initReceiver(){
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_SCREEN_ON);
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
    intentFilter.addAction(Intent.ACTION_USER_PRESENT);
    mContext.registerReceiver(new MyBroadcastReceiver(),intentFilter);
}
private class MyBroadcastReceiver extends BroadcastRecver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action!=null){
            switch (action){
                case Intent.ACTION_SCREEN_ON:
                    Log.d(TAG,"屏幕开启,变亮");
                    break;
                case Intent.ACTION_SCREEN_OFF:
                    Log.d(TAG,"屏幕关闭,变黑");
                    break;
                case Intent.ACTION_USER_PRESENT:
                    Log.d(TAG,"屏幕解开");
                    break;
                default:
                    break;
            }
        }
    }
}

 

你可能感兴趣的:(Android,Android手机屏幕状态,手机屏幕状态,Android手机屏幕状态变化,屏幕手机状态)