小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

android獲取wifi的IP,子網(wǎng)掩碼,網(wǎng)關(guān),dns等信息 | 學(xué)步園

 WindySky 2016-07-15

使用 DhcpInfo 獲取這些信息,返回的是一個(gè)int型數(shù)據(jù),轉(zhuǎn)換下得到想要的信息,代碼如下:

public class MainActivity extends Activity {
	
	private WifiManager my_wifiManager;
	private WifiInfo wifiInfo;
	private DhcpInfo dhcpInfo;

	private TextView tvResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		my_wifiManager = ((WifiManager) getSystemService("wifi"));
		dhcpInfo = my_wifiManager.getDhcpInfo();
		wifiInfo = my_wifiManager.getConnectionInfo();

		tvResult = (TextView) findViewById(R.id.tvResult);
	}

	@Override
	protected void onResume() {
		super.onResume();
		StringBuilder sb = new StringBuilder();
		sb.append("網(wǎng)絡(luò)信息:");
		sb.append("\nipAddress:" + intToIp(dhcpInfo.ipAddress));
		sb.append("\nnetmask:" + intToIp(dhcpInfo.netmask));
		sb.append("\ngateway:" + intToIp(dhcpInfo.gateway));
		sb.append("\nserverAddress:" + intToIp(dhcpInfo.serverAddress));
		sb.append("\ndns1:" + intToIp(dhcpInfo.dns1));
		sb.append("\ndns2:" + intToIp(dhcpInfo.dns2));
		sb.append("\n");
		System.out.println(intToIp(dhcpInfo.ipAddress));
		System.out.println(intToIp(dhcpInfo.netmask));
		System.out.println(intToIp(dhcpInfo.gateway));
		System.out.println(intToIp(dhcpInfo.serverAddress));
		System.out.println(intToIp(dhcpInfo.dns1));
		System.out.println(intToIp(dhcpInfo.dns2));
		System.out.println(dhcpInfo.leaseDuration);

		sb.append("Wifi信息:");
		sb.append("\nIpAddress:" + intToIp(wifiInfo.getIpAddress()));
		sb.append("\nMacAddress:" + wifiInfo.getMacAddress());
		tvResult.setText(sb.toString());
	}

	private String intToIp(int paramInt) {
		return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."
				+ (0xFF & paramInt >> 24);
	}
}

效果如下:

備注:

android.net.NetworkUtils

    /**
     * Convert a IPv4 address from an integer to an InetAddress.
     * @param hostAddress an int corresponding to the IPv4 address in network byte order
     */
    public static InetAddress intToInetAddress(int hostAddress) {
        byte[] addressBytes = { (byte)(0xff & hostAddress),
                                (byte)(0xff & (hostAddress >> 8)),
                                (byte)(0xff & (hostAddress >> 16)),
                                (byte)(0xff & (hostAddress >> 24)) };

        try {
           return InetAddress.getByAddress(addressBytes);
        } catch (UnknownHostException e) {
           throw new AssertionError();
        }
    }

附件下載:

http://download.csdn.net/detail/ttdevs/5805127

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多