首页 > 代码库 > Android 锁屏监听
Android 锁屏监听
package com.example.lockscreenlistenerdemo;public interface LockScreenListener { String Tag="LockScreenListener"; public void onScreenOn(); public void onScreenOff(); public void onUserPresent();}
做一个监听接口。
监听广播,监听锁屏 解锁 点亮屏幕的Action
package com.example.lockscreenlistenerdemo;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class LockScreenBroadcastReceiver extends BroadcastReceiver { LockScreenListener lockScreenListener; public LockScreenBroadcastReceiver(LockScreenListener lockScreenListener) { super(); this.lockScreenListener = lockScreenListener; } @Override public void onReceive(Context content, Intent intent) { // TODO Auto-generated method stub String action=intent.getAction(); if(action.equals(Intent.ACTION_SCREEN_ON)){ lockScreenListener.onScreenOn(); }else if(action.equals(Intent.ACTION_SCREEN_OFF)){ lockScreenListener.onScreenOff(); }else if(action.equals(Intent.ACTION_USER_PRESENT)){ lockScreenListener.onUserPresent(); } }}
接口的实现
package com.example.lockscreenlistenerdemo;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.util.Log;public class MainActivity extends Activity { LockScreenBroadcastReceiver lockScreenBroadcastReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lockScreenBroadcastReceiver = new LockScreenBroadcastReceiver( new LockScreenListener() { @Override public void onUserPresent() { // TODO Auto-generated method stub Log.i(Tag,"onUserPresent"); } @Override public void onScreenOn() { // TODO Auto-generated method stub Log.i(Tag,"onScreenOn"); } @Override public void onScreenOff() { // TODO Auto-generated method stub Log.i(Tag,"onScreenOff"); } }); IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction(Intent.ACTION_SCREEN_OFF); intentFilter.addAction(Intent.ACTION_SCREEN_ON); intentFilter.addAction(Intent.ACTION_USER_PRESENT); registerReceiver(lockScreenBroadcastReceiver, intentFilter); } @Override protected void onDestroy() { // TODO Auto-generated method stub unregisterReceiver(lockScreenBroadcastReceiver); super.onDestroy(); }}
效果
01-04 08:29:23.432: I/LockScreenListener(12615): onScreenOff01-04 08:29:24.062: I/LockScreenListener(12615): onScreenOn01-04 08:29:25.612: I/LockScreenListener(12615): onUserPresent01-04 08:29:35.512: I/LockScreenListener(12615): onScreenOff
Android 锁屏监听
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。