Pages

Monday, July 23, 2012

How to check different Network status in Android?

This code snippet checks which of your networks are available in android.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.coderzheaven.pack;
 
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.widget.Toast;
 
public class CheckConnectionsDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        checkAvailableConnection();
    }
     
    void checkAvailableConnection()
    {
        ConnectivityManager connMgr = (ConnectivityManager)
        this.getSystemService(Context.CONNECTIVITY_SERVICE);   
     
        final android.net.NetworkInfo wifi =
        connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
     
        final android.net.NetworkInfo mobile =
        connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
     
        if( wifi.isAvailable() ){
            Toast.makeText(this, "Wifi Available" , Toast.LENGTH_LONG).show();
        }
        else if( mobile.isAvailable() ){
            Toast.makeText(this, "3G Available" , Toast.LENGTH_LONG).show();
        }
        else{  
            Toast.makeText(this, "No Network Available" , Toast.LENGTH_LONG).show();
        }
    }
}

No comments:

Post a Comment

Popular Posts