// 比如说在button的onclick事件中,要发送一个ontouch的up事件,做法如下: public void clickBtn(View v){ dispatchTouchEvent.(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 240, 400, 0)); } // 期中该btn的onclick事件是在xml里面注册的 <Button android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text="发送onTouch的 UP事件" android:onClick="clickBtn" />
// 截获Home事件做法 // 1.加权限禁止Home键 <uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission> // 2.复写 onAttachedToWindow() @Override public void onAttachedToWindow() { // TODO Auto-generated method stub this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); } // 3.监听onKeyDown事件 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(KeyEvent.KEYCODE_HOME==keyCode) android.os.Process.killProcess(android.os.Process.myPid()); return super.onKeyDown(keyCode, event); }
// android4.0以后屏蔽home键通过 this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); 是不能用的 // 如果是mtk平台的话,可以在setContentView之前getWindow().setFlags(WindowManager.LayoutParams.FLAG_HOMEKEY_DISPATCHED, WindowManager.LayoutParams.FLAG_HOMEKEY_DISPATCHED); //其他平台的需要修改源码,可以先按照第二步的操作,然后修改frameworks/base/services/java/com/android/server/wm/WindowManagerService.java和frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
//打开activity时,需要解锁或者替换系统锁屏,只需要添加两个flag super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); setContentView(R.layout.main);
// 禁止状态栏下拉 StatusBarManager sbm = (StatusBarManager)this.getSystemService(Context.STATUS_BAR_SERVICE); sbm.disable((StatusBarManager.DISABLE_EXPAND)); //需要权限以及系统签名 <uses-permission android:name="android.permission.EXPAND_STATUS_BAR"></uses-permission> <uses-permission android:name="android.permission.STATUS_BAR"></uses-permission>