Android如何打开/关闭系统解锁服务?—典型错误分析

最近正在做一个Android的解锁应用,需要屏蔽系统解锁,并在适当的时候打开系统解锁,在网上search了很多有关系统解锁的资料,学到了很多关于系统解锁方面的知识,同时也发现了很多网友犯下的一个共同的错误。现分享一下:

错误一:

总所周知,要关闭系统自带的锁屏服务需要用到以下代码:

mKeyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
mKeylock = mKeyguard.newKeyguardLock("");
mKeylock.disableKeyguard();

要打开系统锁屏服务需要以下代码:

mKeylock.reenableKeyguard()

网上很多朋友对disableKeyguard()的理解为:将屏幕打开并解锁,只要执行这个方法就会是屏幕变亮并自动解锁!同样reenableKeyguard()的作用是关闭屏幕并上锁!

大错而特错了!

我们来看看google api对这两个方法的解释:

disableKeyguard:Disable the keyguard from showing. If the keyguard is currently showing, hide it. The keyguard will be prevented from showing again untilreenableKeyguard() is called.

reenableKeyguard:Reenable the keyguard. The keyguard will reappear if the previous call todisableKeyguard() caused it it to be hidden.

所以,disableKeyguard只是关闭系统锁屏服务,调用该方法后并不会立即解锁,而是使之不显示解锁,同样reenableKeyguard是恢复锁屏服务,并不会立即锁屏!

错误二:

下面是通过两个按钮来模拟打开/关闭系统锁屏的代码:
      
package com.example.keyguard;

import android.os.Bundle;
import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;


public class KeyGuardActivity extends Activity {

	private Button myButtonOn, myButtonOff;
	private KeyguardManager km;
	
	private final String TAG = "KeyGuardTest";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_guard);
        
        /* 获取KeyGuardManager对象 */
        km = (KeyguardManager)this.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);     
        
        myButtonOff = (Button)findViewById(R.id.buttonOff);
        myButtonOff.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View arg0) {
				/* 关闭系统锁屏服务 */	
                km.newKeyguardLock(TAG).disableKeyguard();   
			} }); 
        myButtonOn = (Button)findViewById(R.id.buttonOn); 
        myButtonOn.setOnClickListener(new Button.OnClickListener(){public void onClick(View arg0) {
        		/* 打开系统锁屏服务 */
				km.newKeyguardLock(TAG).reenableKeyguard();   
        } 
        }); 
    }
  }

 
这段代码看起来很完美,但是执行之后会发现:只能关闭锁屏服务,无法恢复锁屏服务!
是什么原因呢?
原来是在调用disableKeyguard和reenableKeyguard的时候必须使用同一个KeyguardLock对象!
程序略加修改:
package com.example.keyguard;

import android.os.Bundle;
import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;

/**
 * @author Onejune
 * @function 打开/关闭系统锁屏服务测试
 * @note 在打开/关闭系统锁屏服务时必须使用同一个KeyguardLock对象,否则出错
 */
public class KeyGuardActivity extends Activity {

	private Button myButtonOn, myButtonOff;
	private KeyguardManager km;
	private KeyguardLock kl;
	
	private final String TAG = "KeyGuardTest";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_guard);
        
        /* 获取KeyGuardManager对象 */
        km = (KeyguardManager)this.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);     

        /* 获取KeyguardLock对象 */
        kl = km.newKeyguardLock(TAG);
        
        myButtonOff = (Button)findViewById(R.id.buttonOff);
        myButtonOff.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View arg0) {
				/* 关闭系统锁屏服务 */
				kl.disableKeyguard();   
			}
        });
        
        myButtonOn = (Button)findViewById(R.id.buttonOn);
        myButtonOn.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View arg0) {
				/* 打开系统锁屏服务 */
				kl.reenableKeyguard();   
			}
        });
    }
}

终于OK!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值