アンドロイドで。モバイルデータがオンかオフかを確認します

Aug 19 2020

私のアプリには、ユーザーの携帯電話がモバイルデータをオンにしているかどうかを確認する機能が必要です。

私はこのリンクを参照しました:#32239785

これがそのトピックで提供されているコードです

boolean mobileYN = false;

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (tm.getSimState() == TelephonyManager.SIM_STATE_READY) {
    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
    {
        mobileYN = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 1) == 1;
    }
    else{
        mobileYN = Settings.Secure.getInt(context.getContentResolver(), "mobile_data", 1) == 1;
    }
}

このコードは私の携帯電話のほとんどで機能します。

「ノキア8」(Android9)を除く

私もモバイルデータをオフにしました。この関数は引き続きtrueを返します。

どうして?

回答

marmor Aug 20 2020 at 11:36

モバイルデータの設定が有効か無効かを本当に確認する必要がありますか、それともデバイスが現在モバイルデータに接続されているかどうかを確認する必要がありますか?

後者の場合はCONNECTIVITY_SERVICE、ドキュメントの例を使用する必要があります。

private static final String DEBUG_TAG = "NetworkStatusExample";
...
ConnectivityManager connMgr =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isWifiConn = false;
boolean isMobileConn = false;
for (Network network : connMgr.getAllNetworks()) {
    NetworkInfo networkInfo = connMgr.getNetworkInfo(network);
    if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        isWifiConn |= networkInfo.isConnected();
    }
    if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        isMobileConn |= networkInfo.isConnected();
    }
}
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);

上記のドキュメントにもリンクして、あなたは以下のようにチェックアウトする可能性のある他の関連するクラスへのリンクを持っているNetworkInfo.DetailedStateし、ConnectivityManager.NetworkCallback

DiwakarSingh Aug 20 2020 at 11:40

それが役立つ場合は、これを確認してください:

class InternetNetwork {
companion object {
    fun isOnline(context: Context): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val connectivityManager =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val capabilities =
                connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
            if (capabilities != null)
                when {
                    capabilities.hasTransport(TRANSPORT_CELLULAR) -> {
                        Log.i("Internet", "NetworkCapabilities.TRANSPORT_CELLULAR")
                        return true
                    }
                    capabilities.hasTransport(TRANSPORT_WIFI) -> {
                        Log.i("Internet", "NetworkCapabilities.TRANSPORT_WIFI")
                        return true
                    }
                    capabilities.hasTransport(TRANSPORT_ETHERNET) -> {
                        Log.i("Internet", "NetworkCapabilities.TRANSPORT_ETHERNET")
                        return true
                    }
                }
        } else {
            val connectivityManage =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val netInfo = connectivityManage.activeNetworkInfo
            return netInfo != null && netInfo.isConnected
        }

        return false
    }
}
}