你的位置:首页 > 操作系统

[操作系统]android 管理wifi


activity_main.

<??><LinearLayout  ="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <Button    android:id="@+id/openwifi"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="开启WiFi" />  <Button    android:id="@+id/closewifi"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="关闭WiFi" />  <Button    android:id="@+id/wifistate"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="WiFi状态" /></LinearLayout>

package com.example.yanlei.wifi;import android.content.Context;import android.net.wifi.WifiManager;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity {  private Button btnOpen = null;  private Button btnClose = null;  private Button btnState = null;  private WifiManager wifi = null;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    btnOpen = (Button) super.findViewById(R.id.openwifi);    btnClose = (Button) super.findViewById(R.id.closewifi);    btnState = (Button) super.findViewById(R.id.wifistate);    //获取Wifi服务    wifi = (WifiManager) super.getSystemService(Context.WIFI_SERVICE);    //开启    btnOpen.setOnClickListener(new OnClickListener() {      public void onClick(View v) {        wifi.setWifiEnabled(true);        Toast.makeText(getApplicationContext(), "开启Wifi服务!", Toast.LENGTH_LONG).show();      }    });    //关闭    btnClose.setOnClickListener(new OnClickListener() {      public void onClick(View v) {        wifi.setWifiEnabled(false);        Toast.makeText(getApplicationContext(), "关闭Wifi服务!", Toast.LENGTH_LONG).show();      }    });    //状态    btnState.setOnClickListener(new OnClickListener() {      public void onClick(View v) {        Toast.makeText(getApplicationContext(), "Wifi状态:" + String.valueOf(wifi.getWifiState()), Toast.LENGTH_LONG).show();      }    });  }}

权限

<??><manifest ="http://schemas.android.com/apk/res/android"  package="com.example.yanlei.wifi" >  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  <application    android:allowBackup="true"    android:icon="@mipmap/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></manifest>

参考:http://blog.csdn.net/jianghuiquan/article/details/8641123