本文介绍了以编程方式获取网关和子网掩码的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取网关和Android的子网掩码信息?

How can I get gateway and subnet mask details in Android?

推荐答案

我已经找到了一个名为 DhcpInfo android.net 包。它具有存储当前的网络参数的值,一些公共变量。但问题是,他们返回整数8位转换的值移动过的二进制。

I have found a class called DhcpInfo within the android.net package. It has some public variables that stores the values of current Network parameters. But the problem is they return the value in integer converted from 8Bit shifted binary.

样品图片描述你方案:

* *下面是一个示例code:

的Java文件:**

package com.schogini.dhcp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.net.*;
import android.net.wifi.WifiManager;

public class dhcpInfo extends Activity {
    public String   s_dns1 ;
    public String   s_dns2;
    public String   s_gateway;
    public String   s_ipAddress;
    public String   s_leaseDuration;
    public String   s_netmask;
    public String   s_serverAddress;
    TextView info;
    DhcpInfo d;
    WifiManager wifii;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);
        d=wifii.getDhcpInfo();

        s_dns1="DNS 1: "+String.valueOf(d.dns1);
        s_dns2="DNS 2: "+String.valueOf(d.dns2);
        s_gateway="Default Gateway: "+String.valueOf(d.gateway);
        s_ipAddress="IP Address: "+String.valueOf(d.ipAddress);
        s_leaseDuration="Lease Time: "+String.valueOf(d.leaseDuration);
        s_netmask="Subnet Mask: "+String.valueOf(d.netmask);
        s_serverAddress="Server IP: "+String.valueOf(d.serverAddress);

        //dispaly them
        info= (TextView) findViewById(R.id.infolbl);
        info.setText("Network Info\n"+s_dns1+"\n"+s_dns2+"\n"+s_gateway+"\n"+s_ipAddress+"\n"+s_leaseDuration+"\n"+s_netmask+"\n"+s_serverAddress);
    }
}

XML编码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.schogini.dhcp"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".dhcpInfo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>

我试图将整数值转换为其等效,但我不能。如果你这样做,你可以回发..再见..

I tried converting the integer value to its equivalent but i couldn't. If you do so you can post back.. Bye..

更新:一些如何成功地将知识产权从整数形式转换为V4格式转换为IPv4格式:

UPDATE: Some how managed to convert the IP to v4 Format from the integer formConversion to IPv4 Format:

public String intToIp(int i) {

   return ((i >> 24 ) & 0xFF ) + "." +
               ((i >> 16 ) & 0xFF) + "." +
               ((i >> 8 ) & 0xFF) + "." +
               ( i & 0xFF) ;
}

图片来源:http://www.bennadel.com/blog/1830-Converting-IP-Addresses-To-And-From-Integer-Values-With-ColdFusion.htm

这篇关于以编程方式获取网关和子网掩码的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:11