Android 去掉SIM卡插拔出现的重启弹窗提示

调试过程中发现,

插入SIM卡会出现弹窗:SIM ADD: Restart your device to access the mobilenetwork.

拔掉SIM卡也会出现弹窗:SIM REMOVE: Restart your device to access the mobilenetwork.

分析下这块的实现,然后准备去掉:

通过排查代码,发现主要实现是在 frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/UiccSlot.java 这个文件中:

java 复制代码
    private void onIccSwap(boolean isAdded) {
        boolean isHotSwapSupported = mContext.getResources().getBoolean(
                R.bool.config_hotswapCapable);
        if (isHotSwapSupported) {
            log("onIccSwap: isHotSwapSupported is true, don't prompt for rebooting");
            return;
        }
		//....
        log("onIccSwap: isHotSwapSupported is false, prompt for rebooting");
        promptForRestart(isAdded);
    }
    
    private void promptForRestart(boolean isAdded) {
        synchronized (mLock) {
			// 监听弹窗的确认事件,重启设备
            // TODO: SimRecords is not reset while SIM ABSENT (only reset while
            //       Radio_off_or_not_available). Have to reset in both both
            //       added or removed situation.
            listener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    synchronized (mLock) {
                        if (which == DialogInterface.BUTTON_POSITIVE) {
                            if (DBG) log("Reboot due to SIM swap");
                            PowerManager pm = (PowerManager) mContext
                                    .getSystemService(Context.POWER_SERVICE);
                            pm.reboot("SIM is added.");
                        }
                    }
                }

            };

			// 下面的代码主要是弹窗显示的处理
            Resources r = Resources.getSystem();
			// 弹窗显示的信息组装
            String title = (isAdded) ? r.getString(R.string.sim_added_title) :
                    r.getString(R.string.sim_removed_title);
            String message = (isAdded) ? r.getString(R.string.sim_added_message) :
                    r.getString(R.string.sim_removed_message);
            String buttonTxt = r.getString(R.string.sim_restart_button);
			// new 一个 dialog出来
            AlertDialog dialog = new AlertDialog.Builder(mContext)
                    .setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(buttonTxt, listener)
                    .create();
            //设置类型
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            //显示 dialog
            dialog.show();
        }
    }
    
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case EVENT_CARD_REMOVED:
                onIccSwap(false);
                break;
            case EVENT_CARD_ADDED:
                onIccSwap(true);
                break;
            default:
                loge("Unknown Event " + msg.what);
        }
    }

触发 handleMessage 的是在 UiccSlot.java 文件中的 update 函数。

看代码中有一个 isHotSwapSupported 的变量判断。可以开启或者屏蔽这个弹窗的处理,找下代码:

java 复制代码
        boolean isHotSwapSupported = mContext.getResources().getBoolean(
                R.bool.config_hotswapCapable);

接下来就简单了,只需要从配置文件中关闭这个选项即可:

xml 复制代码
    <!-- Is the device capable of hot swapping an UICC Card -->
    <bool name="config_hotswapCapable">true</bool>
相关推荐
穷人小水滴42 分钟前
Android (rust) vulkan (JNI) 画一个三角形: VulkanSurfaceView 初始化
android·开发语言·rust·gpu·jni·vulkan·surfaceview
KdanMin1 小时前
高通Android 12 push framework.jar和service.jar
android·java·jar
GEEKVIP4 小时前
手机使用技巧:如何修复变砖的 Android 手机
android·windows·安全·macos·智能手机·电脑·笔记本电脑
木鬼与槐4 小时前
MySQL高阶1988-找出没所学校的最低分数要求
android·前端·mysql
不写八个5 小时前
Python办公自动化教程(002):PDF的拆分与合并
android·python·pdf
编程乐学6 小时前
网络资源模板--Android Studio 飞机大战游戏
android·ide·游戏·毕业设计·android studio·飞机大战·安卓课设
jiet_h6 小时前
如何在 Android 中用 Kotlin 将 dp 转换为 px
android·开发语言·kotlin
hcgeng6 小时前
Android DataBinding的使用
android·gitee·databinding
青穗CherishTang6 小时前
Android 通过自定义注解实现Activity间跳转时登录路由的自动拦截
android·自定义注解·登录路由拦截