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" />

 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/294.html

梦雪

专注于AIDE教程分享

    留下你的评论

    *评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

    相关推荐