You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android移动数据状态检测异常:诺基亚8(Android9)返回值不符

Hey, let's break down why your mobile data detection code isn't working on the Nokia 8 (Android 9) and fix it!

Why Your Code Fails on Nokia 8 (Android 9)

The core problem here is that starting from Android 9 (API 28), the global mobile_data setting you're relying on no longer accurately reflects the actual mobile data toggle state, especially on multi-SIM devices like the Nokia 8.

Your original code checks the global Settings.Global.MOBILE_DATA value (or the old Settings.Secure entry for older Android versions), but starting from Android 8.0, the system introduced fine-grained control for multi-SIM setups. The global setting now only acts as a default, while each SIM card has its own independent mobile data toggle. Nokia's custom ROM on Android 9 likely adheres strictly to this design, so the global setting doesn't sync with the actual toggle state of the active SIM card.

Also, TelephonyManager.getSimState() only tells you if the SIM is ready, not whether mobile data is enabled for that specific SIM.

Fixes: More Reliable Detection Methods

Here are two robust approaches to resolve this issue:

1. Check if Mobile Data is Actively Connected (Using ConnectivityManager)

This method directly checks if there's an active mobile data connection, which is more aligned with real-world usage scenarios:

private boolean isMobileDataActive(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connManager == null) return false;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        NetworkCapabilities capabilities = connManager.getNetworkCapabilities(connManager.getActiveNetwork());
        return capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
    } else {
        NetworkInfo mobileNetworkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return mobileNetworkInfo != null && mobileNetworkInfo.isConnected();
    }
}

Don't forget to add this permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

2. Check Mobile Data Toggle for the Active SIM Card

If you specifically need to check whether the mobile data toggle is enabled (not just if it's currently connected), use multi-SIM-aware APIs:

private boolean isMobileDataEnabledForActiveSim(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager == null) return false;
        // Get TelephonyManager instance for the active data subscription
        int activeSubId = SubscriptionManager.getDefaultDataSubscriptionId();
        TelephonyManager simTelephonyManager = telephonyManager.createForSubscriptionId(activeSubId);
        return simTelephonyManager.isDataEnabled();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
        if (subscriptionManager == null) return false;
        List<SubscriptionInfo> activeSubs = subscriptionManager.getActiveSubscriptionInfoList();
        if (activeSubs != null) {
            int activeSubId = SubscriptionManager.getDefaultDataSubscriptionId();
            for (SubscriptionInfo info : activeSubs) {
                if (info.getSubscriptionId() == activeSubId) {
                    return Settings.Global.getInt(context.getContentResolver(),
                            "mobile_data_" + info.getSubscriptionId(), 1) == 1;
                }
            }
        }
        return false;
    } else {
        // Fallback for single-SIM devices pre-Android 5.1
        return Settings.Global.getInt(context.getContentResolver(), "mobile_data", 1) == 1;
    }
}

For this method, you'll need the READ_PHONE_STATE permission (and remember to request it dynamically on Android 6.0+):

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Wrap-up

Your original code only checks the global mobile data setting, which doesn't account for multi-SIM devices' per-card controls. The Nokia 8's Android 9 ROM follows the newer system design strictly, causing the mismatch. Using either the ConnectivityManager approach to check active connections, or the multi-SIM-aware toggle check, will resolve this issue.

内容的提问来源于stack exchange,提问作者CC.Wang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 21:37:50