一、编写布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"/>
<Button
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开WiFi网卡"/>
<Button
android:id="@+id/stopButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开WiFi网卡"/>
<Button
android:id="@+id/checkButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开WiFi网卡"/>
</LinearLayout>
二、编写主程序文件MainActivity.java
package com.example.mpcc.zidingyichuankou;
import android.annotation.SuppressLint;
import android.content.Context;
import android.drm.DrmStore;
import android.net.wifi.WifiManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Button startButton = null;
private Button stopButton = null;
private Button checkButton = null;
WifiManager wifiManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button)findViewById(R.id.startButton);
stopButton = (Button)findViewById(R.id.stopButton);
checkButton = (Button)findViewById(R.id.checkButton);
startButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ServiceCast")
@Override
public void onClick(View v) {
wifiManager=(WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
System.out.println("wifi state --->"+wifiManager.getWifiState());
Toast.makeText(MainActivity.this,"当前网卡状态为:"+wifiManager.getWifiState(),Toast.LENGTH_SHORT).show();
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wifiManager=(WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
System.out.println("wifi state --->"+wifiManager.getWifiState());
Toast.makeText(MainActivity.this,"当前网卡状态为:"+wifiManager.getWifiState(),Toast.LENGTH_SHORT).show();
}
});
checkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wifiManager=(WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE);
System.out.println("wifi state --->"+wifiManager.getWifiState());
Toast.makeText(MainActivity.this,"当前网卡状态为:"+wifiManager.getWifiState(),Toast.LENGTH_SHORT).show();
}
});
}
}
三、声明Wi-Fi权限
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>