android 彻底关闭应用程序 返回键的捕获
来源:互联网 发布:dnf几分钟网络连接中断 编辑:程序博客网 时间:2024/06/05 17:14
在开发android应用时,常常通过按返回键(即keyCode == KeyEvent.KEYCODE_BACK)就能关闭程序,其实大多情况下该应用还在任务里运行着,其实这不是我们想要的结果。
我们可以这样做,当用户点击自定义的退出按钮或返回键时(需要捕获动作),我们在onDestroy()里强制退出应用,或直接杀死进程,具体操作代码如下:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//按下键盘上返回按钮
if(keyCode == KeyEvent.KEYCODE_BACK){
new AlertDialog.Builder(this)
.setIcon(R.drawable.services)
.setTitle(R.string.prompt)
.setMessage(R.string.quit_desc)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
}).show();
return true;
}else{
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
System.exit(0);
//或者下面这种方式
//android.os.Process.killProcess(android.os.Process.myPid());
}
- Android 捕获返回键彻底关闭应用程序
- android 彻底关闭应用程序 返回键的捕获
- Android 彻底关闭应用程序 返回键的捕获
- android 彻底关闭应用程序及返回键的捕获
- Android 彻底关闭应用程序,返回键的捕获及AlertDialog
- android 彻底关闭应用程序 返回键的捕获
- 彻底关闭应用程序 返回键的捕获
- 彻底关闭android应用程序
- android 彻底关闭应用程序
- android--彻底关闭--应用程序
- android 彻底关闭应用程序
- Android彻底关闭应用程序
- android 彻底关闭应用程序
- android 彻底关闭应用程序
- android 彻底关闭应用程序
- Android 彻底关闭应用程序
- android 返回键的捕获
- 【Android】按Back键彻底关闭一个应用程序
- Hadoop学习<三>--前两节回顾
- 深入浅出Java多线程程序设计
- hdu 1796 How many integers can you find【容斥原理】
- android的PowerManager和PowerManager.WakeLock
- socket阻塞与非阻塞,同步与异步
- android 彻底关闭应用程序 返回键的捕获
- android使用mat分析内存泄漏
- Android—自定义ActionBar标题与菜单中的文字样式
- IOS 关闭键盘 退出键盘 的5种方式
- log4j 日志路径配置
- Linux Note – 通过ulimit和PAM来限制资源
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- 常用JavaScript技巧
- Android连续点击两次Back键退出程序