伙计我有一个获取android的ip地址的活动,但是给了我错误的ip。我在Genymotion中测试它,因为它在Virtualbox上运行并获得了一个ip。 http://pctechtips.org/data/ipAddressAndroid.png我正在研究一个可以显示android的tcp / ip配置的类。但我认为ip部分是错误的。任何帮助赞赏 tcpConfigActivity
package com.example.george.droidnet;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
/**
* Created by george on 2/7/17.
*/
public class TcpConfigActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.output);
//reference textView text config
TextView tcpText = (TextView) findViewById(R.id.text_output);
tcpText.setText("TCP/IP Configuration: ");
TextView tcpOutput = (TextView) findViewById(R.id.config_output);
tcpOutput.setText(getIpConfig());
}
public String getIpConfig() {
WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
return Integer.toString(ip);
}
}
这是output.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_light"
android:divider="@drawable/vertical_divider1"
android:orientation="vertical"
android:showDividers="middle"
tools:context="com.example.george.droidnet.MainActivity"
android:weightSum="1">
<TextView
android:id="@+id/hostname"
android:layout_height="100dp"
android:layout_width="match_parent"
android:background="@color/primary"
android:textSize="24sp"
/>
<TextView
android:id="@+id/text_output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextStyle"
android:text="output1" />
<TextView
android:id="@+id/config_output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/txtOutStyle"
android:text="output2"/>
</LinearLayout>