android 阻止返回退出

一、重写返回按钮

复制代码
//2024-12-02
@Override
public void onBackPressed() {
    // 在这里添加您的拦截逻辑
   // Toast.makeText(this, "返回键被拦截", Toast.LENGTH_SHORT).show();
  //  super.onBackPressed();
    showExitDialog();
}

二、弹出提示

复制代码
private void showExitDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("屏触科技")
                .setMessage("是否退出客户端?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击确定按钮,不做任何操作
                        Cyber_Public_Var.m_cpu.onDestroy();
                        // 在这里添加退出应用的相关代码
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.show();
    }
相关推荐
巧克力芋泥包1 天前
前端使用阿里云图形验证码;并且与安卓进行交互
android·前端·阿里云
Just_Paranoid1 天前
【MQTT】基于 Android 设备接入物联网平台最佳实践
android·mqtt·eclipse·iot·paho·mqtt.fx
alexhilton1 天前
深入理解withContext和launch的真正区别
android·kotlin·android jetpack
TDengine (老段)1 天前
TDengine 转换函数 TO_JSON 用户手册
android·大数据·数据库·json·时序数据库·tdengine·涛思数据
q***42821 天前
SpringCloudGateWay
android·前端·后端
卫生纸不够用1 天前
Appium-锁屏-Android
android·appium
阿拉斯攀登1 天前
安卓工控机 OTA 升级方案(SpringBoot+MQTT)
android·spring boot·物联网·iot
顾林海1 天前
从0到1搭建Android网络框架:别再让你的请求在"路上迷路"了
android·面试·架构
花花鱼1 天前
android room中实体类变化以后如何迁移
android
Jomurphys1 天前
设计模式 - 适配器模式 Adapter Pattern
android