I have been using the following code (acquired from the web) to check for Internet connection in my Android (web) app.
import android.content.Context;
import android.net.ConnectivityManager;
public class DetectConnection {
public static boolean checkInternetConnection(Context context) {
ConnectivityManager con_manager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
return (con_manager.getActiveNetworkInfo() != null
&& con_manager.getActiveNetworkInfo().isAvailable()
&& con_manager.getActiveNetworkInfo().isConnected());
}
}
Android studio is now giving me deprecation warnings concerning the use of getActiveNetworkInfo()
. A simple search in the Internet shows that I should be using the API in [1]. I however could not figure out how to do it. Any ideas?
[1] https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback