android强制锁屏代码,Android监听锁屏、解锁、开屏功能代码
1、⾸先定义ScreenListener
;
astReceiver;
t;
;
Filter;
anager;
/**
*Createdby${zyj}on2016/6/21.
*/
publicclassScreenListener{
privateContextmContext;
privateScreenBroadcastReceivermScreenReceiver;
privateScreenStateListenermScreenStateListener;
publicScreenListener(Contextcontext){
mContext=context;
mScreenReceiver=newScreenBroadcastReceiver();
}
/**
*screen状态⼴播接收者
*/
privateclassScreenBroadcastReceiverextendsBroadcastReceiver{
privateStringaction=null;
@Override
publicvoidonReceive(Contextcontext,Intentintent){
action=ion();
if(_SCREEN_(action)){//开屏
enOn();
}elif(_SCREEN_(action)){//锁屏
enOff();
}elif(_USER_(action)){//解锁
Prent();
}
}
}
/**
*开始监听screen状态
*
*@paramlistener
*/
publicvoidbegin(ScreenStateListenerlistener){
mScreenStateListener=listener;
registerListener();
getScreenState();
}
/**
*获取screen状态
*/
privatevoidgetScreenState(){
PowerManagermanager=(PowerManager)mContext
.getSystemService(_SERVICE);
if(enOn()){
if(mScreenStateListener!=null){
enOn();
}
}el{
if(mScreenStateListener!=null){
enOff();
}
}
}
/**
*停⽌screen状态监听
*/
publicvoidunregisterListener(){
sterReceiver(mScreenReceiver);
}
/**
*启动screen状态⼴播接收器
*/
privatevoidregisterListener(){
IntentFilterfilter=newIntentFilter();
ion(_SCREEN_ON);
ion(_SCREEN_OFF);
ion(_USER_PRESENT);
erReceiver(mScreenReceiver,filter);
}
publicinterfaceScreenStateListener{//返回给调⽤者屏幕状态信息
publicvoidonScreenOn();
publicvoidonScreenOff();
publicvoidonUrPrent();
}
}
2、使⽤
;
patActivity;
;
;
publicclassMainActivityextendsAppCompatActivity{
privateScreenListenerscreenListener;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
te(savedInstanceState);
tContentView(ty_main);
screenListener=newScreenListener();
(StateListener(){
@Override
publicvoidonScreenOn(){
xt(,"屏幕打开了",_SHORT).show();
}
@Override
publicvoidonScreenOff(){
xt(,"屏幕关闭了",_SHORT).show();
}
@Override
publicvoidonUrPrent(){
xt(,"解锁了",_SHORT).show();
}
});
}
}
以上所述是⼩编给⼤家介绍的Android监听锁屏、解锁、开屏功能代码的相关知识,希望对⼤家有所帮助!
本文发布于:2023-03-15 07:33:58,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1678836839260903.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:安卓锁屏.doc
本文 PDF 下载地址:安卓锁屏.pdf
留言与评论(共有 0 条评论) |