Android打开关闭wifi
package com.mycompany.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.net.wifi.*;
import android.content.*;
import android.provider.*;

public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//打开wifi设置界面
public void openwifi(View view)
{
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(intent);

}
//开启wifi
public void startwifi(View view)
{
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(true);
}
//关闭wifi
public void closewifi(View view)
{
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(false);
}
}

添加权限

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
评论 抢沙发

请登录后发表评论