System - Get IP Address

//Check the internet connection.

@SuppressWarnings("deprecation")
private void NetwordDetect() {
    boolean WIFI = false;
    boolean MOBILE = false;
    android.net.ConnectivityManager CM = (android.net.ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo[] networkInfo = CM.getAllNetworkInfo();
    for (android.net.NetworkInfo netInfo : networkInfo) {
          if (netInfo.getTypeName().equalsIgnoreCase("WIFI")) {
              if (netInfo.isConnected()) {
                  WIFI = true;
              }
          }
          if (netInfo.getTypeName().equalsIgnoreCase("MOBILE")) {
              if (netInfo.isConnected()) {
                  MOBILE = true;
              }
          }
    }
    if(WIFI == true) {
       final String IPaddress = GetDeviceipWiFiData();
       textview1.setText(IPaddress);
   }
   if(MOBILE == true) {
      final String IPaddress = GetDeviceipMobileData();
      textview1.setText(IPaddress);
  }
}

public String GetDeviceipMobileData(){
 try {
       for (Enumeration<java.net.NetworkInterface> en = java.net.NetworkInterface.getNetworkInterfaces();
             en.hasMoreElements();) {
             java.net.NetworkInterface networkinterface = en.nextElement();
          for (Enumeration<java.net.InetAddress> enumIpAddr = networkinterface.getInetAddresses(); enumIpAddr.hasMoreElements();) {
             java.net.InetAddress inetAddress = enumIpAddr.nextElement();
             if (!inetAddress.isLoopbackAddress()) {
                 return inetAddress.getHostAddress().toString();
            }
         }
      }
 } catch (Exception ex) {
      textview1.setText("Current IP" + ex.toString());
 }
 return null;
}

public String GetDeviceipWiFiData() {
 android.net.wifi.WifiManager wm = (android.net.wifi.WifiManager) getSystemService(WIFI_SERVICE);
 @SuppressWarnings("deprecation")
 String ip = android.text.format.Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
 return ip;
}


//Add Permission to manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>


//On Button Access
NetwordDetect();

Komentar

Cara pembuatan Apps paling Populer

Create Stopwatch App in Android using Sketchware

How to enable download in webview in Sketchware apps?

TextInputLayout in Sketchware

Create Stopwatch App in Android using Sketchware

How to integrate Admob Ads in Sketchware project using AIDE?

How to find and​ highlight a word in a text field in Sketchware?

Code for implementing Notifications in Sketchware

A Flash Light App in Sketchware

Create a Stopwatch App using Chronometer in Sketchware

Create app to save all passwords, protected using voice key