-1

How can I check if my device already connected or not?

Here is code I used on my Main Menu:

ConnectivityManager connManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo m3g = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mWifi.isConnected() || m3g.isConnected()) {
        try {
            db.Connect();
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if (mWifi.isConnected() == false & m3g.isConnected() == false) { 
          //alert
      }         

It work perfectly but when you quit app and didn't logout, the next time you open application it will open the activity you leave last time not the Main Menu so i can't check connection

So I want to know is there anyway I can alway check device connection ?

Sorry for my English

Huy
  • 179
  • 1
  • 1
  • 9
  • Ping a well known server like google? – takendarkk Jul 27 '16 at 17:33
  • What do you mean by quit app? Do you mean switch to another app or close the app by swiping it off the list of active apps? – Kariem Jul 27 '16 at 17:33
  • i mean press home button. So the next time you open, it will resume on where you leave so my code can't check if connection is available or not – Huy Jul 27 '16 at 17:35
  • What i want to do is like you play online game, if you connection is lost, the application will instantly report it to you – Huy Jul 27 '16 at 17:36
  • Check this link : http://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app – Sagar Jogadia Jul 27 '16 at 17:38

1 Answers1

0

The onResume() method is called when your application is getting resumed again. So you can place code here that will log the user out and restart the process for checking for connectivity.

You can see documentation for all activity methods here

Kariem
  • 750
  • 5
  • 13