Android判断屏幕开关状态

 

方法一:使用系统服务

PowerManager pm= (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);

        if(!pm.isScreenOn())
        {
            //熄屏状态
        }

 

方法二:使用广播

public class ScreenReceiver extends BroadcastReceiver {




    @Override
    public void onReceive(Context context, Intent intent) {




        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            
//收到亮屏广播

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

//收到熄屏广播

        }


    }
    }
//注册广播
ScreenReceiver sOnBroadcastReciver=new ScreenReceiver();
        IntentFilter recevierFilter=new IntentFilter();
        recevierFilter.addAction(Intent.ACTION_SCREEN_ON);
        recevierFilter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(sOnBroadcastReciver, recevierFilter);


//取消注册
if(sOnBroadcastReciver!=null)
        {
            unregisterReceiver(sOnBroadcastReciver);
        }

 

注意:亮屏和熄屏广播只能通过动态注册才能收到。

 

posted on 2015-12-01 09:59  屌丝迷途  阅读(1696)  评论(0编辑  收藏  举报

导航