MainActivity.java
package com.example.wifi;
/*本文主要简单地编写设备wifi开关
* @kongchengjiumeng
* 主要通过wifimanage来实现
* 最后一定记得在androidmainfest中注册四个permition
* */
import android.app.Activity;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.nfc.cardemulation.OffHostApduService;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button mBtn_ONwifi;
private Button mBtn_OFFwifi;
private Button mBtn_CHECKwifi;
private WifiManager wifimanager = null;
//
设置了三个按钮,并且分别设置了监听器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtn_ONwifi = (Button) findViewById(R.id.btn_onwifi);
mBtn_OFFwifi = (Button) findViewById(R.id.btn_offwifi);
mBtn_CHECKwifi = (Button) findViewById(R.id.btn_checkwifi);
mBtn_ONwifi.setOnClickListener(new OnWifiListener());
mBtn_OFFwifi.setOnClickListener(new OffWifiListener());
mBtn_CHECKwifi.setOnClickListener(new CheckWifiListener());
}
//
用于启动wifi的监听器
public class OnWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//
从content中获得wifi_service的object,并且向下转型为wifimanager
wifimanager = (WifiManager) MainActivity.this.getSystemService(MainActivity.this.WIFI_SERVICE);
//
启动wifi开光
wifimanager.setWifiEnabled(true);
//
显示text
Toast.makeText(MainActivity.this, "wifi已经打开了,请叫我天才", Toast.LENGTH_SHORT).show();
}
}
//
用于关闭wifi的监听器
class OffWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//
从content中获得wifi_service的object,并且向下转型为wifimanager
wifimanager = (WifiManager) MainActivity.this.getSystemService(MainActivity.WIFI_SERVICE);
//
关闭wifi的开光
wifimanager.setWifiEnabled(false);
//
显示text
Toast.makeText(MainActivity.this, "wifi已经关闭,请叫我天才", Toast.LENGTH_SHORT).show();
}
}
//
设置检查wifi状态的监听器
public class CheckWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//
从content中获得wifi_service的object,并且向下转型为wifimanager
wifimanager = (WifiManager) MainActivity.this.getSystemService(MainActivity.this.WIFI_SERVICE);
switch (wifimanager.getWifiState()) {
case 0:
Toast.makeText(MainActivity.this, "wifi目前处于正在关闭状态", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this, "wifi目前处于关闭状态", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(MainActivity.this, "wifi目前处于正在打开状态", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this, "wifi目前处于打开状态", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(MainActivity.this, "wifi目前处于未知状态", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
}
activity_main.xml
<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:orientation="vertical">
<Button
android:id="@+id/btn_onwifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_onwifi"/>
<Button
android:id="@+id/btn_offwifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_offwifi"/>
<Button
android:id="@+id/btn_checkwifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_checkwifi"/>
</LinearLayout>
androidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wifi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
</manifest>