Android ScreenLockReceiver监听锁屏功能示例

本文实例讲述了Android ScreenLockReceiver监听锁屏功能。分享给大家供大家参考,具体如下:

监听屏幕锁屏状态(注册接受者--执行业务--注销接受者)

public class AppLockService extends Service {

private ActivityManager am;

private KeyguardManager keyguardManager;

private LockScreenReceiver receiver;

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);

//注册锁屏广播接收者

IntentFilter filter = new IntentFilter();

filter.addAction(Intent.ACTION_SCREEN_OFF);

receiver = new LockScreenReceiver();

registerReceiver(receiver, filter);

new Thread(){

public void run() {

//一直在监听锁屏动作,一旦监听到,便执行操作

while(true){

boolean isLockScreen = keyguardManager.inKeyguardRestrictedInputMode();

/**此处可作相应操作*/

}

};

}.start();

}

private final class LockScreenReceiver extends BroadcastReceiver{

@Override

public void onReceive(Context context, Intent intent) {

tempAppLocks.clear();

}

}

@Override

public void onDestroy() {

super.onDestroy();

//取消锁屏的广播监听

unregisterReceiver(receiver);

}

}

希望本文所述对大家Android程序设计有所帮助。

以上是 Android ScreenLockReceiver监听锁屏功能示例 的全部内容, 来源链接: utcz.com/z/358528.html

回到顶部