/*****************************MainActivity ***************************************/
package com.cs.dxm.wifitest; import android.content.Context; import android.net.wifi.WifiManager; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.Toast; import java.sql.BatchUpdateException; public class MainActivity extends AppCompatActivity { Button btnOpenWifi; Button btnStopWifi; Button btnCheckWifi; WifiManager wifiManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); btnOpenWifi = (Button)findViewById(R.id.btnOpenWifi); btnOpenWifi.setOnClickListener(new OpenWifiClickListener()); btnStopWifi=(Button)findViewById(R.id.btnStopWifi); btnStopWifi.setOnClickListener(new StopWifiClickListener()); btnCheckWifi=(Button)findViewById(R.id.btnCheckWifi); btnCheckWifi.setOnClickListener(new CheckWifiClickListener()); } class OpenWifiClickListener implements View.OnClickListener{ @Override public void onClick(View v) { wifiManager = (WifiManager)MainActivity.this.getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(true); System.out.println("Wifi state is "+wifiManager.getWifiState()); Toast.makeText(MainActivity.this,"当前WiFi状态为:"+wifiManager.getWifiState(),Toast.LENGTH_SHORT); } } class StopWifiClickListener implements View.OnClickListener{ @Override public void onClick(View v) { wifiManager = (WifiManager)MainActivity.this.getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(false); System.out.println("Wifi state is "+wifiManager.getWifiState()); Toast.makeText(MainActivity.this,"当前WiFi状态为:"+wifiManager.getWifiState(),Toast.LENGTH_SHORT); } } class CheckWifiClickListener implements View.OnClickListener{ @Override public void onClick(View v) { System.out.println("Wifi state is "+wifiManager.getWifiState()); Toast.makeText(MainActivity.this,"当前WiFi状态为:"+wifiManager.getWifiState(),Toast.LENGTH_SHORT); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
/***************************activity_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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnOpenWifi" android:text="Open Wifi" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnStopWifi" android:text="Stop Wifi" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnCheckWifi" android:text="Check Wifi" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
/*******************************AndroidManifest.xml**********************************************/
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cs.dxm.wifitest"> <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" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <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> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> </manifest>