欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 监听屏幕是否锁屏的实例代码

程序员文章站 2023-12-20 08:48:28
今天,简单讲讲如何监听手机屏幕是否锁屏。 实现方法: 1)通过broadcastreceiver接收广播intent.action_screen_on和intent.a...

今天,简单讲讲如何监听手机屏幕是否锁屏。

实现方法:

1)通过broadcastreceiver接收广播intent.action_screen_on和intent.action_screen_off可以判断屏幕状态是否锁屏,但是只有屏幕状态发生改变时才会发出广播;

2)如果要在屏幕状态发生改变之前就想获取屏幕状态,可以通过反射机制调用powermanager的isscreenon方法 。

具体实现,见代码:

直接上代码:

1.定义一个接收广播的类

package com.app.lib; 
import android.content.broadcastreceiver; 
import android.content.context; 
import android.content.intent; 
import android.content.intentfilter; 
import android.os.powermanager; 
/** 
 * created by ${zyj} on 2016/6/21. 
 */ 
public class screenlistener { 
  private context mcontext; 
  private screenbroadcastreceiver mscreenreceiver; 
  private screenstatelistener mscreenstatelistener; 
  public screenlistener(context context) { 
    mcontext = context; 
    mscreenreceiver = new screenbroadcastreceiver(); 
  } 
  /** 
   * screen状态广播接收者 
   */ 
  private class screenbroadcastreceiver extends broadcastreceiver { 
    private string action = null; 
    @override 
    public void onreceive(context context, intent intent) { 
      action = intent.getaction(); 
      if (intent.action_screen_on.equals(action)) { // 开屏 
        mscreenstatelistener.onscreenon(); 
      } else if (intent.action_screen_off.equals(action)) { // 锁屏 
        mscreenstatelistener.onscreenoff(); 
      } else if (intent.action_user_present.equals(action)) { // 解锁 
        mscreenstatelistener.onuserpresent(); 
      } 
    } 
  } 
  /** 
   * 开始监听screen状态 
   * 
   * @param listener 
   */ 
  public void begin(screenstatelistener listener) { 
    mscreenstatelistener = listener; 
    registerlistener(); 
    getscreenstate(); 
  } 
  /** 
   * 获取screen状态 
   */ 
  private void getscreenstate() { 
    powermanager manager = (powermanager) mcontext 
        .getsystemservice(context.power_service); 
    if (manager.isscreenon()) { 
      if (mscreenstatelistener != null) { 
        mscreenstatelistener.onscreenon(); 
      } 
    } else { 
      if (mscreenstatelistener != null) { 
        mscreenstatelistener.onscreenoff(); 
      } 
    } 
  } 
  /** 
   * 停止screen状态监听 
   */ 
  public void unregisterlistener() { 
    mcontext.unregisterreceiver(mscreenreceiver); 
  } 
  /** 
   * 启动screen状态广播接收器 
   */ 
  private void registerlistener() { 
    intentfilter filter = new intentfilter(); 
    filter.addaction(intent.action_screen_on); 
    filter.addaction(intent.action_screen_off); 
    filter.addaction(intent.action_user_present); 
    mcontext.registerreceiver(mscreenreceiver, filter); 
  } 
  public interface screenstatelistener {// 返回给调用者屏幕状态信息 
    public void onscreenon(); 
    public void onscreenoff(); 
    public void onuserpresent(); 
  } 
} 

2、使用:

package com.app.lib; 
import android.support.v7.app.appcompatactivity; 
import android.os.bundle; 
import android.widget.toast; 
public class mainactivity extends appcompatactivity { 
  private screenlistener screenlistener ; 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    screenlistener = new screenlistener( mainactivity.this ) ; 
    screenlistener.begin(new screenlistener.screenstatelistener() { 
      @override 
      public void onscreenon() { 
        toast.maketext( mainactivity.this , "屏幕打开了" , toast.length_short ).show(); 
      } 
      @override 
      public void onscreenoff() { 
        toast.maketext( mainactivity.this , "屏幕关闭了" , toast.length_short ).show(); 
      } 
      @override 
      public void onuserpresent() { 
        toast.maketext( mainactivity.this , "解锁了" , toast.length_short ).show(); 
      } 
    }); 
  } 
} 

总结

以上所述是小编给大家介绍的android 监听屏幕是否锁屏的实例代码,希望对大家有所帮助

上一篇:

下一篇: