2019年12月19日 星期四

檢查連線類型

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



public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int connectNum = 0;

        ConnectivityManager connMgr =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        for (Network network : connMgr.getAllNetworks()) {
            NetworkCapabilities networkInfo = connMgr.getNetworkCapabilities(network);
            if (networkInfo.hasTransport (NetworkCapabilities.TRANSPORT_ETHERNET) == true ) {
                connectNum++;
            }
        }

        Log.d("haha check connect", "num="+connectNum);
    }
}

沒有留言:

張貼留言