简体   繁体   中英

How to check device is just connected to the Internet

Is there a way to check when a device, which has not been connected to the Internet , is just connected. If I connect the phone to the Internet inside my application, I should do some actions (eg Toast ).

Note: It is not the same question with Detect if Android device has Internet connection and Android - checking if the device is connected to the internet .

Try this:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

You will also need:

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

in your android manifest.

Note that having an active network interface doesn't guarantee that a particular networked service is available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM