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): onScreenOff 01-04 08:29:24.062: I/LockScreenListener(12615): onScreenOn 01-04 08:29:25.612: I/LockScreenListener(12615): onUserPresent 01-04 08:29:35.512: I/LockScreenListener(12615): onScreenOff
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。