android 4.0 禁用系统home键!亲测可用!
来源:互联网 发布:ie修复软件 编辑:程序博客网 时间:2024/05/16 09:56
网上找到:
刚刚发现4.0上还有一种方法可以屏蔽和重写Home键,而且非常简单。代码如下:
public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);//关键代码
setContentView(R.layout.main);
}
再重写onKey事件即可。
但是放在手机上测试大多都不行!只能再找!嘻嘻!
找了好久才找到的!亲测可用!
public class HomeKeyLocker { private OverlayDialog mOverlayDialog; public void lock(Activity activity) { if (mOverlayDialog == null) { mOverlayDialog = new OverlayDialog(activity); mOverlayDialog.show(); } } public void unlock() { if (mOverlayDialog != null) { mOverlayDialog.dismiss(); mOverlayDialog = null; } } private static class OverlayDialog extends AlertDialog { public OverlayDialog(Activity activity) { super(activity, R.style.OverlayDialog); WindowManager.LayoutParams params = getWindow().getAttributes(); params.type = TYPE_SYSTEM_ERROR; params.dimAmount = 0.0F; // transparent params.width = 0; params.height = 0; params.gravity = Gravity.BOTTOM; getWindow().setAttributes(params); getWindow().setFlags(FLAG_SHOW_WHEN_LOCKED | FLAG_NOT_TOUCH_MODAL, 0xffffff); setOwnerActivity(activity); setCancelable(false); } public final boolean dispatchTouchEvent(MotionEvent motionevent) { return true; } protected final void onCreate(Bundle bundle) { super.onCreate(bundle); FrameLayout framelayout = new FrameLayout(getContext()); framelayout.setBackgroundColor(0); setContentView(framelayout); } }}
public class MainActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener { private ToggleButton mTbLock; private HomeKeyLocker mHomeKeyLocker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHomeKeyLocker = new HomeKeyLocker(); mTbLock = (ToggleButton) findViewById(R.id.tb_lock); mTbLock.setOnCheckedChangeListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (buttonView == mTbLock) { if (isChecked) { mHomeKeyLocker.lock(this); } else { mHomeKeyLocker.unlock(); } } } @Override protected void onDestroy() { super.onDestroy(); mHomeKeyLocker.unlock(); mHomeKeyLocker = null; }}
<style name="OverlayDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:backgroundDimEnabled">false</item> </style>
项目:https://github.com/shaobin0604/Android-HomeKey-Locker
别忘了加上权限
<uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW"/>
想要研究的自己研究吧!
感谢这位大神!
0 0
- android 4.0 禁用系统home键!亲测可用!
- 禁用home 4.0+ ,亲测可用
- android 4.0 禁用系统home键
- android 4.0.3 禁用系统home键
- android 4.0 禁用系统home键
- 禁用Android系统Home键
- android 4.0 禁用系统home键(续)
- 4.0以上系统锁屏类应用禁用home键
- android 禁用home键盘
- android 禁用home键盘
- android系统捕获home键
- Android屏蔽HOME键亲测可用
- Android4.4 home键禁用
- Android开发-监听系统的 home键
- Android系统中home键的监听
- Ubuntu 16.04 禁用guest账户,亲测可用
- 禁用BAMBOOK S1的home键
- 亲测可用Android原生代码添加系统服务
- Linux下获取binary中的rpath信息
- AWR阅读
- TCP和UDP的区别
- hdu 2159 FATE 二维背包
- Web service是什么?
- android 4.0 禁用系统home键!亲测可用!
- zendstudio配置大全
- AngryBot Demo 缓存池应用
- 将路径数组变为统计数组
- Android.mk文件详解介绍
- Android adb无线连接设备
- unity3d调用手机或电脑摄像头
- javaBean学习总结
- 我的PHP工作日志