Help us understand the problem. What is going on with this article?

51歳からのプログラミング 備忘 Androidのネットワーク接続の確認

NetworkInfoはAPI29から非推奨になりました。
テキストはNetworkInfoを使っているので代替手段を探しました。
めっちゃ時間かかって、もうAndroidとかPHPとかLaravelとか何者なのか忘れてしまいました。


さて、リファレンスでは、ConnectivityManager.NetworkCallback APIをよく読んで、ConnectivityManager#getNetworkCapabilities とか ConnectivityManager#getLinkProperties とかを検討してね!ってことらしいので、getNetworkCapabilities でいいのかな?

ConnectivityManager.getNetworkCapabilities

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ネットワークの接続状態を知らせるクラスオブジェクト manager を取得!
        // getActiveNetwork で、現在デフォルトで接続してるネットワークオブジェクトを取得!
        // capabilities 無線接続ポイントや接続に関するキャリアのサービス、通信接続のタイプなど、いろんな接続情を確認できる!
        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        Network network = manager.getActiveNetwork();
        NetworkCapabilities capabilities = manager.getNetworkCapabilities(network);

        // wifiだったら
        if(capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)){
            Log.d("TRANSPORT","WIFI");
        }
        // 携帯通信だったら
        if(capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)){
            Log.d("TRANSPORT","CELLULAER");
        }
        Log.d("TRANSPORT", String.valueOf(capabilities));
    }
}

ネット接続で確認したい状態定数(NetworkCapabilities.TRANSORT_CELLULARとか)は

https://developer.android.com/reference/android/net/NetworkCapabilities.html#TRANSPORT_WIFI

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away