当前位置: 代码迷 >> Android >> android 4.0 障蔽home键实现
  详细解决方案

android 4.0 障蔽home键实现

热度:28   发布时间:2016-05-01 20:28:49.0
android 4.0 屏蔽home键实现

最近研究锁屏,按照常规的方法,常规方法如下

public void onAttachedToWindow(){         this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);            super.onAttachedToWindow(); 	} 

?但是这个方法到了4.0的系统上就失效了,按home照样会返回桌面。apktool了几个锁屏的软件之后发现其实现方法,即用WindowManager的addview方法将view加到窗口上,加上的时候将view的layoutparamas的type设为LayoutParams.TYPE_SYSTEM_ERROR,这个值sdk的解释为

?

public static final int TYPE_SYSTEM_ERROR Since: API Level 1 Window type: internal system error windows, appear on top of everything they can. 

?如果直接这样用会报错,还要在AndroidManifest.xml里面加上权限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>,ok上完整代码,封装了一个类,其中Lock和hide方法实现锁屏和解锁。

public class LockLayer {	private Activity mActivty;	private WindowManager mWindowManager;	private View mLockView;	private LayoutParams mLockViewLayoutParams;		public LockLayer(Activity act) {		mActivty = act;		init();	}	private void init(){		mWindowManager = mActivty.getWindowManager();		mLockViewLayoutParams = new LayoutParams();		mLockViewLayoutParams.width = LayoutParams.MATCH_PARENT;		mLockViewLayoutParams.height = LayoutParams.MATCH_PARENT;		//实现关键		mLockViewLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;		//apktool value,这个值具体是哪个变量还请网友帮忙		mLockViewLayoutParams.flags = 1280;	}	public void lock() {		if(mLockView!=null){			mWindowManager.addView(mLockView, mLockViewLayoutParams);		}	}	public void unlock() {		if(mWindowManager!=null){			mWindowManager.removeView(mLockView);		}	}	public void setLockView(View v){		mLockView = v;	}}

?使用方法如下

public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        View lock = View.inflate(this, R.layout.main, null);        LockLayer lockLayer = new LockLayer(this);        lockLayer.setLockView(lock);        lockLayer.lock();    }

?

ok,这时可以实现按home键不返回桌面的效果,其他的键需要用另外的代码实现,网上都有。这段代码只调用一次的时候没有问题,但是如果遇到多线程或者多次lock和hide就会有问题,修改如下,一是将LockLayer改为单例模式,二是在hide和lock的时候先判断状态。修改后代码如下

public class LockLayer {	private Activity mActivty;	private WindowManager mWindowManager;	private View mLockView;	private LayoutParams mLockViewLayoutParams;	private static LockLayer mLockLayer;	private boolean isLocked;		public static synchronized LockLayer getInstance(Activity act){		if(mLockLayer == null){			mLockLayer = new LockLayer(act);		}		return mLockLayer;	}		private LockLayer(Activity act) {		mActivty = act;		init();	}	private void init(){		isLocked = false;		mWindowManager = mActivty.getWindowManager();		mLockViewLayoutParams = new LayoutParams();		mLockViewLayoutParams.width = LayoutParams.MATCH_PARENT;		mLockViewLayoutParams.height = LayoutParams.MATCH_PARENT;		//实现关键		mLockViewLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;		//apktool value,这个值具体是哪个变量还请网友帮忙		mLockViewLayoutParams.flags = 1280;	}	public synchronized void lock() {		if(mLockView!=null&&!isLocked){			mWindowManager.addView(mLockView, mLockViewLayoutParams);		}		isLocked = true;	}	public synchronized void unlock() {		if(mWindowManager!=null&&isLocked){			mWindowManager.removeView(mLockView);		}		isLocked = false;	}	public synchronized void setLockView(View v){		mLockView = v;	}}

?还有个问题是mLockViewLayoutParams.flags = 1280;1280这个值具体是哪个变量还不知道,有知道的留个言。

1 楼 fable0618 2012-06-14  
请问这样屏蔽了HOME后还能同时屏蔽statusbar么,有没有什么方法?
  相关解决方案