How to check my internet access on Android?
What I really want is, when the user has no connection, I want to show some dialog that he or she has no connection.
I tried to put this on my MainActivity but still didn't work.
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
I also used this one but it didn't also work:
public class ConnectionChangeReceiver extends BroadcastReceiver
{
@Override
public void onReceive( Context context, Intent intent )
{
Conn开发者_JAVA技巧ectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE );
if ( activeNetInfo != null )
{
Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
if( mobNetInfo != null )
{
Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
}
}
I added this on my manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Can someone tell me what I'm doing wrong, I would really appreciate it a lot. Thanks in advance!
Maybe this link can help you a bit further: Broadcastreceiver to obtain ServiceState information
You could past the mentined CommunicationManager code in your activity, or better, if you need access from more activities, you could also declare the class as static in the Application Singletin instance and call it from there as described here
You also need to register the BroadcastReceiver in your application manifest:
<receiver android:enabled="true" android:name="com.project.mobile.controller.comm.ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
Enjoy
public static boolean haveInternet(Context ctx) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
return true;
}
If the boolean Method returns true then there is Internet connection if false No Connection..
if(haveInternet(ActivityName.this)==true){
//Some code
}else{
Toast.makeText(ActivityName.this,"No Internet Connection", Toast.LENGTH_SHORT).show();
}
Also see this for further help.. Internet Connection...
Just use this method
private boolean isNetworkConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = cm.getActiveNetworkInfo();
if (network != null) {
return network.isAvailable();
}
return false;
}
u can check your inter connection with the help of ConnectivityManager class. i have given one function that u can use in code.
public static boolean
CheckInternet(Context context) {
ConnectivityManager connec =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = > connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile =connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
Here if condition check for wifi and mobile network is available or not. If anyone of them isavailable or connected then it will return true, otherwise false;
if (wifi.isConnected()) {
return true;
} else if
(!mobile.isConnected())
{
return false;
} else if
(mobile.isConnected())
{
return true;
} return false;
}
after using of this function u need 2 just check the condition like
if (CheckInternet(getBaseContext()))
{
//connection successfull
} else
{ Toast.makeText(getBaseContext(),"Please Check InternetConnectionToast.LENGTH_LONG).show();
}
enjoy
The above methods work when you are connected to a Wi-Fi source or via cell phone data packs. But in case of Wi-Fi connection there are cases when you are further asked to Sign-In like in Cafe. So in that case your application will fail as you are connected to Wi-Fi source but not with the Internet.
This method works fine.
public static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager)context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
try {
URL url = new URL("http://www.google.com/");
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
urlc.setRequestProperty("User-Agent", "test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000); // mTimeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
} else {
return false;
}
} catch (IOException e) {
Log.i("warning", "Error checking internet connection", e);
return false;
}
}
return false;
}
Please use this in a separate thread from the main thread as it makes a network call and will throw NetwrokOnMainThreadException if not followed.
精彩评论