Android自定义锁屏

1. 在apk里的AndroidManifest.xml里加入


   
   

2.  注册接受屏幕开关的Receive,其实注册开就可以了。

IntentFilter filter_lock = new IntentFilter();  
        filter_lock.addAction(Intent.ACTION_SCREEN_ON);  
        filter_lock.addAction(Intent.ACTION_SCREEN_OFF); 
        filter_lock.addAction(Intent.ACTION_BOOT_COMPLETED);
        
        registerReceiver(unLockScreenReceiver, filter_lock); 

3. 实现你要做的事情,启动一个解锁界面或者直接自动解锁

BroadcastReceiver unLockScreenReceiver = new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON) 
|| intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
unLockScreen();
}
}
};

4. 实现解锁

protected void unLockScreen() {
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock(""); 
keyguardLock.disableKeyguard(); 
}


附加:

下面的代码可以用来点亮屏幕
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "SimpleTimer");
mWakelock.acquire();


你可能感兴趣的:(Andriod)