在之前的博客文章中,我写了点在 Android 6 系统中连接到指定名称的 Wifi 的体验.然而,在 Android 7 中,有一些东西又变化了.另外就是在那篇文章中我说要提供代码,结果拖到这篇文章,也没有提供.那么,请忽略那篇文章,来看看这篇吧.

不变

首先聊下 Android 6 到 Android 7之间没有变化的一些东西.想要让设备连接到指定名称的热点,那么一定要经历以下的几个过程:

  1. 打开 Wifi
  2. 包装出 WifiConfiguration 对象
  3. WifiConfiguration 对象获得 WifiManager 对象中 WifiConfiguration 的 index
  4. 用 enableNetwork 连接网络
  5. 使用完毕,断开网络然后删除创建出的配置文件
  6. 关闭 Wifi

  1. 在 Android 6 中,如果系统中已经存在了其他的 app 或者是由系统生成的热点信息,那么它将会返回 -1,而在 Android 7上并不是这个设定,是直接返回 index 的值
  2. 在 Android 7 中,无法使用 removeNetwork 这个公开的方法删除一个已有的配置,虽然在删除后,在系统中显示已经被删除,但是当重新关闭再打开 Wifi 开关时,会发现被删除的配置由系统进行创建.这里,需要使用系统的隐藏方法 forget 进行删除

以上就是所有信息了.以下是代码:

package com.xxxxxxxx.wifitest7;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity"; private final static String WIFI_SSID = "TP-LINK_ACA6";
private Button mBtnOpenWifi;
private Button mBtnCloseWifi;
private Button mBtnAddWifi;
private Button mBtnDelWifi;
private Button mBtnConnect;
private Button mBtnDisConnect;
private TextView mTextViewShowResult; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initUi();
} private void initUi() {
mBtnOpenWifi = (Button) findViewById(R.id.btnOpenWifi);
mBtnOpenWifi.setOnClickListener(this);
mBtnCloseWifi = (Button) findViewById(R.id.btnCloseWifi);
mBtnCloseWifi.setOnClickListener(this);
mBtnAddWifi = (Button) findViewById(R.id.btnAddWifi);
mBtnAddWifi.setOnClickListener(this);
mBtnDelWifi = (Button) findViewById(R.id.btnDelWifi) ;
mBtnDelWifi.setOnClickListener(this);
mBtnConnect = (Button) findViewById(R.id.btnConnect);
mBtnConnect.setOnClickListener(this);
mBtnDisConnect = (Button) findViewById(R.id.btnDisConnect);
mBtnDisConnect.setOnClickListener(this);
mTextViewShowResult = (TextView) findViewById(R.id.textViewShowResult);
} @Override
public void onClick(View view) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mTextViewShowResult.setText("");
// "TP-LINK_ACA6"
switch (view.getId()) {
case R.id.btnOpenWifi:
if (openWifi()) {
mTextViewShowResult.setText("正在打开 Wifi,请稍等");
} else {
mTextViewShowResult.setText("打开 Wifi 失败,Wifi 已经开启?");
}
break;
case R.id.btnCloseWifi:
if (closeWifi()) {
mTextViewShowResult.setText("正在关闭 Wifi,请稍等");
} else {
mTextViewShowResult.setText("关闭 Wifi 失败,Wifi 已经关闭?");
}
break;
case R.id.btnAddWifi:
int networkId = addWifiItem(WIFI_SSID);
mTextViewShowResult.setText("得到的 Wifi 的 ID 是:" + networkId);
break;
case R.id.btnDelWifi:
forgetConfiguration(this,createWifiConfiguration(WIFI_SSID));
break;
case R.id.btnConnect:
connectToSpecSsid(addWifiItem(WIFI_SSID));
break;
case R.id.btnDisConnect:
disableSpecSsid(addWifiItem(WIFI_SSID));
break;
}
} public static WifiConfiguration createWifiConfiguration(String SSID) {
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.allowedKeyManagement.clear();
wifiConfiguration.allowedAuthAlgorithms.clear();
wifiConfiguration.hiddenSSID = true; // 无密码
wifiConfiguration.SSID = "\"" + SSID + "\"";
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); return wifiConfiguration;
} private boolean openWifi() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
boolean result = false;
switch (wifiManager.getWifiState()) {
case WifiManager.WIFI_STATE_DISABLED:
case WifiManager.WIFI_STATE_DISABLING:
wifiManager.setWifiEnabled(true);
result = true;
break;
case WifiManager.WIFI_STATE_ENABLED:
case WifiManager.WIFI_STATE_ENABLING:
result = false;
break;
}
return result;
} private boolean closeWifi() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
boolean result = false;
switch (wifiManager.getWifiState()) {
case WifiManager.WIFI_STATE_ENABLED:
case WifiManager.WIFI_STATE_ENABLING:
wifiManager.setWifiEnabled(false);
result = true;
break;
case WifiManager.WIFI_STATE_DISABLED:
case WifiManager.WIFI_STATE_DISABLING:
result = false;
break;
}
return result;
} private int addWifiItem (String ssid) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> wifiConfigurations = wifiManager.getConfiguredNetworks();
int networkId = -10;
if (null != wifiConfigurations) {
for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
if (wifiConfiguration.SSID.equals("\"" + ssid + "\"")) {
networkId = wifiConfiguration.networkId;
break;
}
}
} if (networkId == -10) {
networkId = wifiManager.addNetwork(createWifiConfiguration(ssid));
} return networkId;
} private boolean delWifiItem (String ssid) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> wifiConfigurations = wifiManager.getConfiguredNetworks();
boolean result = false;
int networkId = -10;
if (null != wifiConfigurations) {
for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
if (wifiConfiguration.SSID.equals("\"" + ssid + "\"")) {
networkId = wifiConfiguration.networkId;
Log.i(TAG, "delWifiItem: 找到对应的 ssid,编号为:" + networkId);
break;
}
}
} if (networkId != -10) {
Log.i(TAG, "delWifiItem: 执行删除指定网络的过程");
wifiManager.removeNetwork(networkId);
result = true;
} wifiConfigurations = wifiManager.getConfiguredNetworks();
for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
if (wifiConfiguration.SSID.equals("\"" + ssid + "\"")) {
Log.i(TAG, "delWifiItem: 又找到对应的 ssid,编号为:" + networkId);
result = false;
break;
}
} return result;
} private boolean connectToSpecSsid(int netId) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
return wifiManager.enableNetwork(netId,true);
} private boolean disableSpecSsid(int netId) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
return wifiManager.disableNetwork(netId);
} public static void forgetConfiguration(Context context, WifiConfiguration config) {
if (config == null) {
return;
} WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configuredNetworks = wifiMan.getConfiguredNetworks();
if (configuredNetworks == null) {
Log.e(TAG, "failed to get configured networks");
return;
} for (WifiConfiguration wc : configuredNetworks) {
if (wc != null && wc.SSID != null && TextUtils.equals(wc.SSID, config.SSID)) {
wifiMan.forget(wc.networkId, null);
Log.d(TAG, "forgot network config: " + wc.toString());
break;
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.xxxxxx.wifitest7.MainActivity"> <Button
android:id="@+id/btnOpenWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:text="@string/open_wifi"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/> <Button
android:id="@+id/btnCloseWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginTop="32dp"
android:text="@string/close_wifi"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/> <TextView
android:id="@+id/textViewShowResult"
android:layout_width="321dp"
android:layout_height="220dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="@+id/btnOpenWifi"
app:layout_constraintRight_toRightOf="@+id/btnCloseWifi"
app:layout_constraintTop_toBottomOf="@+id/btnConnect"
app:layout_constraintHorizontal_bias="0.484"/> <Button
android:id="@+id/btnConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:text="@string/connect"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnAddWifi"/> <Button
android:id="@+id/btnDisConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginTop="8dp"
android:text="@string/disconnect"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnDelWifi"/> <Button
android:id="@+id/btnAddWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/addwifi"
app:layout_constraintTop_toBottomOf="@+id/btnOpenWifi"
android:layout_marginStart="32dp"
app:layout_constraintLeft_toLeftOf="parent"/> <Button
android:id="@+id/btnDelWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/delwifi"
app:layout_constraintTop_toBottomOf="@+id/btnCloseWifi"
android:layout_marginEnd="32dp"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>

Android 7.1.1 又出幺蛾子了 —— 再谈 Android 上的 Wifi 连接的更多相关文章

  1. 再谈Android应用瘦身

    Android应用apk安装包的大小,虽然对于现在WiFi普及情况而言消耗流量已经微乎其微,但是,对于一款好的应用,对于一款负责任的应用,当然是越小越好了. 引言: .应用越小,下载越快,也就意味着新 ...

  2. 再谈Android AsyncTask的优缺点

    导语:之前做习惯了Framework层的开发,今天在武汉斗鱼公司面试APP客户端的开发,其中一道题是讲述Asynctask的优缺点,我靠,我只是知道有这么一个东西,会用而已,看来之前的生活太过于安逸, ...

  3. Android配置----小米手机通过wifi连接ADB调试Android应用

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  4. android中常用的弹出提示框

    转自:http://blog.csdn.net/centralperk/article/details/7493731 我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的 ...

  5. 【Android】创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  6. Android 如何解决dialog弹出时无法捕捉Activity的back事件

    Android 如何解决dialog弹出时无法捕捉Activity的back事件 在一些情况下,我们需要捕捉back键事件,然后在捕捉到的事件里写入我们需要进行的处理,通常可以采用下面三种办法捕捉到b ...

  7. Python_内置函数之round的幺蛾子

    pycharm运行结果 1 ret = round(0.5) print(ret) >>> 0 ret1 = round(1.5) print(ret1) >>> ...

  8. xamarin.android 实现 Activity 底部弹出对话框菜单

    Resources/drawable 下新增如下文件: push_bottom_in.xml <?xml version="1.0" encoding="utf-8 ...

  9. Android Activity为什么要细化出onCreate、onStart、onResume、onPause、onStop、onDesdroy这么多方法让应用去重载?

    原文:http://www.xuebuyuan.com/1608083.html 最近在研究Activity的启动流程,老罗的blog在看,也找了其它资料学习,也跟过Android4.3的源码, 在跟 ...

随机推荐

  1. 模板——STL队列

    C++ STL queue 容器优先队列&&队列 队列 #include<queue> #include<iostream> using namespace s ...

  2. linux 学习第十一天

    一.配置服务说明 1.1.linux系统中的一切都是文件 1.2.配置一个服务就是在修改去配置文件 1.3.要想让新的配置文件立即生效,需要重启对应的服务 二.配置网卡 2.1.编辑配置文件 vim ...

  3. javascript json对象操作(基本增删改查)

    /** * Json对象操作,增删改查 * * @author lellansin * @blog www.lellansin.com * @version 0.1 * * 解决一些常见的问题 * g ...

  4. OpenStack部署博客推荐

    OpenStack部署推荐博客 shhnwangjian https://www.cnblogs.com/shhnwangjian/category/942049.html(推荐) 点评: 1.实现过 ...

  5. 从码云把之前的代码git push 回IDEA 对IDEA里的文件进行简单操作

    前情提要:我的IDEA里的项目之前已经和码云连接成功可以上传.但我直接在电脑文件夹里对文件进行重命名.剪切.粘贴等操作之后IDEA对操作后的文件不识别,无奈之下我将码云上之前的代码推回重新新建了项目. ...

  6. SupperSocket深入浅出

    这篇文章出要是SuperSocket底层如何接收数据 Process(ArraySegment<byte> segment) 获取加载数据(直到数据全部接收后返回) namespace S ...

  7. mysql导入报错【The MySQL server is running with the --event-scheduler=DISABLED】

    一.问题: 在进行mysql操作导入库的时候,报出了[The MySQL server is running with the --event-scheduler=DISABLED] 查看后台日志是事 ...

  8. linux 命令缩写

    su super user apt advanced packaging tool ifconfig interface configuration so shared object fsp frac ...

  9. Configure,Makefile.am, Makefile.in, Makefile文件

    一 软件安装关于 makefile文件问题 如果拿到的工程文件中,没有Makefile文件,而只有configure.in和Makefile.am文件,我们是不能够直接进行编译的,必须根据config ...

  10. 学习HTML 第二节.HTML头部

    HTML为什么要有个头部?还不太明白,可能是一些要提前声明的东西吧.先看看有什么内容吧. 可以添加在头部区域的元素标签为: <title>标题,这个我们知道了: <meta>使 ...