Android 代码中实现返回键功能
2015-12-11 11:58
771 查看
代码里实现返回键
刚才自己的项目里用到了有两种办法:
采用
Runtime
[code]/** * Allows Java applications to interface with the environment in which they are running. Applications can not create an instance of this class, but they can get a singleton instance by invoking getRuntime() */ Runtime runtime = Runtime.getRuntime(); try { runtime.exec("input keyevent " + KeyEvent.KEYCODE_BACK); } catch (IOException e) { e.printStackTrace(); }
采用
Instrumentation(注意此方法不能再主线程中)
[code]/** * Base class for implementing application instrumentation code. When running with instrumentation turned on, this class will be instantiated for you before any of the application code, allowing you to monitor all of the interaction the system has with the application. An Instrumentation implementation is described to the system through an AndroidManifest.xml's <instrumentation> tag. */ new Thread(){ @Override public void run() { Instrumentation ins = new Instrumentation(); ins.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); } }.start();
若想在返回的时候干点别的
重写onKeyDown,实现对返回键的监听
[code]@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { //do something return false; } else { return super.onKeyDown(keyCode, event); } }
相关文章推荐
- ubuntu 14kylin下编译Android5.1源码
- android4.4系统源码编译(二)_源码下载
- Android 动画介绍(一)
- React Native Android 代码中设置访问js 的地址
- Android之屏幕适配问题
- android:imeOptions属性介绍
- Android之双击回到顶部
- 自定义Android圆形进度条(附源码)
- Android 开发之serviceManager分析
- android java socket通信乱码
- Android实现图片轮播切换实例代码
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- android内存优化发展——使用软引用
- android适配器Adapter
- Android TextView : “Do not concatenate text displayed with setText”
- Android Studio 项目代码全部消失--出现原因及解决方法
- Android中OnTouchListener多点触摸
- studio报com.android.dex.DexException: Multiple dex files define Lcom/umeng/analytics错误
- android动画详解
- Android EditText屏蔽默认长按粘贴复制事件