1

I've searched this and I can't resolve the problem, look at this code checked on other examples:

public class EntryActivity extends Activity {

public Boolean isNetAvailable(Context con)  {

    try{
        ConnectivityManager connectivityManager = (ConnectivityManager)       
                                                                  con.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileInfo =connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
            return true;
        }
    }
    catch(Exception e){
       e.printStackTrace();
    }
    return false;

this method works well when I call it in onCreate method, like this:

if (isNetAvailable(getApplicationContext())) {
        Toast.makeText(EntryActivity.this,
            "You have Internet Connection, wait a moment to sincronize" +
            "Metar's stations", Toast.LENGTH_LONG)
               .show();
         Intent myIntent = new Intent(this, MainActivity.class);
         startActivity(myIntent);

    } else {
        Toast.makeText(EntryActivity.this,
            "You Do not have Internet Connection",
                Toast.LENGTH_LONG).show();

        EntryActivity.this.startActivity(new Intent(
                Settings.ACTION_WIRELESS_SETTINGS));
            }

This only works for the first Internet connection Check! the problem is, if Wifi is off the user should leaves the app or turns Wi-Fi on.But, if He turns it on, I don't have access to that change and I can't advance to my other activity. So, If in the beggining the internet connection status is down, i want keep checking if the User turns it On to advanve to other activity. What can I do?

cpfp
  • 377
  • 1
  • 4
  • 15

3 Answers3

0

You don't need to keep checking for Internet connection changes via executing your code at intervals. Instead you need to define a broadcast recievers in your app to listen network changes.

See this answer How can I monitor the network connection status in Android?

Community
  • 1
  • 1
Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
  • humm.. but there's no way to perform a task to keep updating the Internet connection? – cpfp Apr 10 '13 at 12:01
  • yes. you don't need to do any operation to check internet connection status. Let your reciever listen for statis change. and in its onRecieve store the state to any of your variable in your app that your code can refer to inside the if check before doing any operation. simple – Rohit Sharma Apr 10 '13 at 12:04
  • I'm new at android programming and I'm getting confused to implement a broadcast receiver in my code.. can you tell me how can I call a broadcastReceiver in OnCreate??;) – cpfp Apr 10 '13 at 14:28
0

In you android manifest:

<receiver android:name="com.softteco.clivero.receivers.ConnectionReceiver" >
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

And create class:

public class ConnectionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean newConnectionState = isConnected(context);
    }
    private boolean isConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
        boolean ifConnected = false;
        for (int i = 0; i < info.length; i++) {
            NetworkInfo connection = info[i];
            if (connection.getState().equals(NetworkInfo.State.CONNECTED)) {
                ifConnected = true;
            }
        }
        return ifConnected;
    }

}

It's one of the possible variants. Hope it will help you.

Nickolai Astashonok
  • 2,878
  • 3
  • 20
  • 23
  • let's try, I will answer soon ;) – cpfp Apr 10 '13 at 13:57
  • tell me just one thing,how can I apply this example in my code? – cpfp Apr 10 '13 at 14:24
  • Hm, in you activity you should register some custom broadcast receiver, which will get intents from ConnectionReceiver#onReceive method. And depends on broadcast, that ConnectionReceiver sent show appropriate activity. Have you got the basic idea? – Nickolai Astashonok Apr 10 '13 at 14:30
  • hmm I'm understanding. But the step of move to the another activity if online like you can see in my code: EntryActivity.this.startActivity(new Intent( Settings.ACTION_WIRELESS_SETTINGS)); Should be done in the onReceive? – cpfp Apr 10 '13 at 14:44
  • I mean, this part of the code: Intent myIntent = new Intent(this, MainActivity.class); startActivity(myIntent); – cpfp Apr 10 '13 at 15:11
  • It's better to do in EntryActivity. – Nickolai Astashonok Apr 11 '13 at 06:26
0

this may helps you Monitor network connections Wi-Fi, GPRS, UMTS, etc

public boolean isNetworkAvailable(Context context)
    {
        ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        return info!=null;
    }

also make sure you added require permission in Manifest File like

    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    </uses-permission>
Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45