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);
}

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

上一篇:安装、配置、启动FTP、SSH或NFS服务


下一篇:把.Net开发环境迁移到Linux上去