简体   繁体   中英

How can my android app detect range of ip address

I want my android app to detect a range of ip address basically, from 192.168.150.0 - .255 and i want it to put it in void onStart, i created an array but it doesnt work.

here is my java class for the main activity

MediaPlayer playMusic;

@Override    
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    for(int array;array < 256;array++)
    try {

        if("192.168.150")
    }            
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    //playMusic.release();
}

/**
 * Touch listener to use for in-layout UI controls to delay hiding the
 * system UI. This is to prevent the jarring behavior of controls going away
 * while interacting with activity UI.
 */
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (AUTO_HIDE) {
            delayedHide(AUTO_HIDE_DELAY_MILLIS);
        }
        return false;
    }
};

Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
        mSystemUiHider.hide();
    }
};

/**
 * Schedules a call to hide() in [delay] milliseconds, canceling any
 * previously scheduled calls.
 */
private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
}

}

and this is my java class to detect wifi when the app is open

public class WiFiChangeBroadcastReceiver extends BroadcastReceiver {
    private String LOGTAG = getClass().getSimpleName();

    @Override 
    public void onReceive(Context context, Intent intent) {
        Log.d(LOGTAG, "WiFi Status Changed");
        if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            NetworkInfo networkInfo = intent
                    .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (networkInfo.isConnected()) {
                Log.d(LOGTAG,
                        "Wifi is connected: " + String.valueOf(networkInfo));
            }
        }
    }
}

Use this to get the IP Address :

public String getIpAddr() {
   WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
   WifiInfo wifiInfo = wifiManager.getConnectionInfo();
   int ip = wifiInfo.getIpAddress();

   String ipString = String.format(
   "%d.%d.%d.%d",
   (ip & 0xff),
   (ip >> 8 & 0xff),
   (ip >> 16 & 0xff),
   (ip >> 24 & 0xff));

   return ipString;
}

Please Note : You need to add android.permission.INTERNET and android.permission.ACCESS_WIFI_STATE in your AndroidManifest.xml as <user-permission/> to access the code.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Reference : https://stackoverflow.com/a/7975955/1239966

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM