- 关于监听按键的事件响应:
/******add wc 180516****start***/ private void goHome(){ Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 注意 intent.addCategory(Intent.CATEGORY_HOME); startActivity(intent); } //keyEvent public void simulateKey(final int KeyCode) { new Thread() { public void run() { try { Instrumentation inst = new Instrumentation(); inst.sendKeyDownUpSync(KeyCode); } catch (Exception e) { Log.e("Exception when sendKeyDownUpSync", e.toString()); } } }.start(); } public void simulateKeyEvent(final KeyEvent event) { new Thread() { public void run() { try { Instrumentation inst = new Instrumentation(); inst.sendKeySync(event); } catch (Exception e) { Log.e("Exception when sendKeyDownUpSync", e.toString()); } } }.start(); } /******add wc 180516****end***/
- 通过按键事件的传入直接就可以相对应按键的方法
simulateKey(KeyEvent.KEYCODE_BACK);//BACK事件
simulateKey(KeyEvent.KEYCODE_HOME);
- onTouch事件与onclick事件的冲突:
/******add wc180516****start***/
//如果初始落点与松手落点的坐标差值超过6个像素,则拦截该点击事件
//否则继续传递,将事件交给OnClickListener函数处理
// lastX = (int) event.getRawX();
// lastY = (int) event.getRawY();
// final int x = (int) event.getRawX();
// final int y = (int) event.getRawY();
if (Math.abs(x - lastX) > 6 && Math.abs(y - lastY) > 6) {
return true;
}
/******add wc 180516****end***/
注意:当View同时进行onclick与onLongclick,onLongClick返回为true 避免执行完onLongClick事件再次出发onClick事件
- 判断某个Activity是否在前台显示:
private ComponentName getTopActivity() {
final ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
ActivityInfo aInfo = null;
List<RunningTaskInfo> list = am.getRunningTasks(1);
if (list != null && list.size() != 0) {
RunningTaskInfo topRunningTask = list.get(0);
return topRunningTask.topActivity;
} else {
return null;
}
}
//eg:
private boolean isCamera(){
ComponentName cn = getTopActivity();
Log.i(TAG, "interceptKeyTi isCamera cn = "+cn);
if(cn != null && "com.android.camera.CameraLauncher".equals(cn.getClassName())){
return true;
}
return false;
}
- 判断当前是否在Home:
private boolean isCurrentHomeActivity(ComponentName component, ActivityInfo homeInfo) {
if (homeInfo == null) {
final PackageManager pm = mContext.getPackageManager();
homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
.resolveActivityInfo(pm, 0);
}
return homeInfo != null && component != null
&& homeInfo.packageName.equals(component.getPackageName())
&& homeInfo.name.equals(component.getClassName());
}
- 关于虚拟按键的显示与隐藏:
final Object mServiceAquireLock = new Object();
IStatusBarService getStatusBarService() {
synchronized (mServiceAquireLock) {
if (mStatusBarService == null) {
mStatusBarService = IStatusBarService.Stub.asInterface(
ServiceManager.getService("statusbar"));
}
return mStatusBarService;
}
}
//BEGIN hide/show NavigationBar
private void showNavigationBar(){
mHandler.post(new Runnable() {
@Override
public void run() {
try {
IStatusBarService statusbar = getStatusBarService();
if (statusbar != null) {
statusbar.showNavigationBar();
}
} catch (RemoteException e) {
// re-acquire status bar service next time it is needed.
mStatusBarService = null;
}
}
});
}
//END hide/show NavigationBar