本博客将通过一个实例向大家介如何开发一个简易的小应用实现获取IP地址和MAC地址。(需要注意的一点是,虚拟机可能不支持WIFI操作,所以需要用真机进行测试),由于许多解释我已经在源代码中解释的挺清楚了,所以不做过多的解释,如果不懂的小伙伴,可以留言交流。
APP的界面如下:
1、需要在AndroidManifest.xml文件中进行权限声明
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hjw.mywifi">
<!--注意注意!!!!!!!!!此处开始添加权限 -->
<!--允许应用程序改变网络连接状态-->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<!--允许应用程序改变wifi连接状态-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<!--允许应用程序获取网络连接状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--允许应用程序获取wifi连接状态-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2、设置布局界面activity_main.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:layout_marginLeft="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvWifiState"
android:layout_marginTop="15dp"
android:textSize="15dp"
android:text="Wifi状态:"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="本地IP地址:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvIPAddress"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="本地MAC地址:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMACAddress"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="SSID:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvSSID"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="连接速度:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="@+id/tvLinkSpeed"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnEnableWiFi"
android:layout_marginRight="10dp"
android:text="开启WiFi网卡"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnDisableWiFi"
android:layout_marginRight="10dp"
android:text="关闭WiFi网卡"/>
</LinearLayout>
3、定义一个类(MyWifiInfo.java)用于记录WiFi下的设备IP地址、MAC地址等信息。
package com.example.hjw.mywifi;
/**
* created by hjw on 2018-11-20
* 定义一个类用于记录WiFi下的设备IP地址,MAC地址、网络的SSID及连接速度
*/
public class MyWifiInfo {
public int WifiState;
public String IPAddress;//IP地址
public String MacAddress;//MAC地址
public String SSID;//SSID
public int LinkSpeed;//连接速度
}
4、在MainActivity.java类中进行操作。
package com.example.hjw.mywifi;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* created by hjw on 2018-11-20
*
*/
public class MainActivity extends AppCompatActivity {
private static TextView tvWifiState; //显示wifi状态
private static TextView tvIPAddress; //显示IP地址
private static TextView tvMACAddress; //显示MAC地址
private static TextView tvSSID; //显示网络SSID
private static TextView tvLinkSpeed; //显示连接速度
private Button btnEnableWiFi,btnDisableWiFi; //打开及关闭按钮
private WifiManager wifiManager=null; //WiFi管理对象
private Thread myWifiInfoThread=null; //查询wifi状态信息的线程
private static MyWifiInfo myWiFi=null; //记录wifi信息的对象
private static Handler handler=new Handler(); //用户将状态信息更新到到界面UI线程中
/**WIFI的开启与关闭*/
Button.OnClickListener buttonListener=new Button.OnClickListener(){
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.btnEnableWiFi:
wifiManager=(WifiManager)MainActivity.this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
break;
case R.id.btnDisableWiFi:
wifiManager=(WifiManager)MainActivity.this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
break;
}
}
};
/**WIFI状态检测*/
public MyWifiInfo getMyWifiInfo(Context context)
{
MyWifiInfo myWifiInfo=new MyWifiInfo();
//获取wifi管理对象
WifiManager wifi=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
//获取wifi连接状态
myWifiInfo.WifiState=wifi.getWifiState();
if (myWifiInfo.WifiState==3)
{
//获取wifi信息对象
WifiInfo info=wifi.getConnectionInfo();
//获取SSID
myWifiInfo.SSID=info.getSSID();
//获取本地IP地址
int ipAddress=info.getIpAddress();
myWifiInfo.IPAddress=intToIp(ipAddress);
//获取本地MAC地址
myWifiInfo.MacAddress=info.getMacAddress();
//获取网络速度
myWifiInfo.LinkSpeed=info.getLinkSpeed();
}
return myWifiInfo;
}
//将整数的IP地址转换为点分十进制表示的IP地址
public String intToIp(int i){
return (i&0xFF)+"."+((i>>8)&0xFF)+"."+((i>>16)&0xFF)+"."+((i>>24)&0xFF);
}
//查询WIFI状态的线程
private Runnable inquireWork=new Runnable() {
@Override
public void run() {
try{
while (!Thread.interrupted()){
//查询wifi状态及信息
MyWifiInfo theWFInfo=getMyWifiInfo(MainActivity.this);
//将查询到的信息更新到主界面控件
MainActivity.UpdateWifiInfo(theWFInfo);
Thread.sleep(1000);//休眠1s
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
public static void UpdateWifiInfo(MyWifiInfo object){
myWiFi=object;
handler.post(RefreshWiFiInfoCtrl);//用post()方法将更新控件的线程发送给主线程
}
//对主线程中的控件进行更新线程
private static Runnable RefreshWiFiInfoCtrl=new Runnable() {
@Override
public void run() {
if (myWiFi.WifiState==0)
tvWifiState.setText("WIFI状态:正在关闭...");
else if (myWiFi.WifiState==1)
tvWifiState.setText("WIFI状态:关闭");
else if (myWiFi.WifiState==2)
tvWifiState.setText("WIFI状态:正在打开...");
else if (myWiFi.WifiState==3)
tvWifiState.setText("WIFI状态:打开");
else
tvWifiState.setText("WIF状态:未知");
if (myWiFi.WifiState==3)
{
tvIPAddress.setText(myWiFi.IPAddress);
tvMACAddress.setText(myWiFi.MacAddress);
tvSSID.setText(myWiFi.SSID);
tvLinkSpeed.setText(Integer.toString(myWiFi.LinkSpeed)+"Mbps");
}
else
{
tvIPAddress.setText(" ");
tvMACAddress.setText(" ");
tvSSID.setText(" ");
tvLinkSpeed.setText(" ");
}
}
};
/**在onCreate()函数中创建WIFI查询线程,在onStart()函数中启动查询线程,在onDestory()函数中销毁查询线程*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvWifiState=(TextView)findViewById(R.id.tvWifiState);
tvIPAddress=(TextView)findViewById(R.id.tvIPAddress);
tvMACAddress=(TextView)findViewById(R.id.tvMACAddress);
tvSSID=(TextView)findViewById(R.id.tvSSID);
tvLinkSpeed=(TextView)findViewById(R.id.tvLinkSpeed);
btnEnableWiFi=(Button)findViewById(R.id.btnEnableWiFi);
btnDisableWiFi=(Button)findViewById(R.id.btnDisableWiFi);
//创建查询wifi状态的线程
myWifiInfoThread=new Thread(null,inquireWork,"InquireWiFiThread");
}
@Override
protected void onDestroy(){
super.onDestroy();
myWifiInfoThread.interrupt();//终止线程
}
@Override
protected void onStart(){
super.onStart();
//启动线程
if (!myWifiInfoThread.isAlive()){
myWifiInfoThread.start();
}
}
}