话不多说,直接上代码了,这示例是直接调用原生接口实现的,没有使用反射的方式,如果找不到接口无法编译,请依赖一下对应系统的framewords.jar,并且参考我https://www.cnblogs.com/huhe/p/15848891.html文章的说明配置一下androidStudio编译环境即可

MainActivity.java
  1 package com.xxx.wifi_ap;
2
3 import static android.net.ConnectivityManager.TETHERING_WIFI;
4
5 import androidx.appcompat.app.AppCompatActivity;
6
7 import android.content.BroadcastReceiver;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.net.ConnectivityManager;
12 import android.net.wifi.WifiConfiguration;
13 import android.net.wifi.WifiManager;
14 import android.os.Bundle;
15 import android.os.Handler;
16 import android.os.Looper;
17 import android.util.Log;
18 import android.view.MotionEvent;
19 import android.view.View;
20 import android.view.inputmethod.InputMethodManager;
21 import android.widget.Button;
22 import android.widget.EditText;
23 import android.widget.LinearLayout;
24 import android.widget.TextView;
25 import android.widget.Toast;
26
27 public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {
28
29 private static final IntentFilter WIFI_INTENT_FILTER = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
30 private static final String TAG = "MainActivity";
31 private ConnectivityManager mConnectivityManager;
32 private WifiManager mWifiManager;
33 private final ConnectivityManager.OnStartTetheringCallback mOnStartTetheringCallback =
34 new ConnectivityManager.OnStartTetheringCallback() {
35 @Override
36 public void onTetheringFailed() {
37 super.onTetheringFailed();
38 Log.e(TAG,"onTetheringFailed!!!");
39 }
40 };
41
42 private LinearLayout root_layout;
43 private Button mAPEnable;
44 private EditText mEditName;
45 private EditText mEditPwd;
46 private TextView mApMsg;
47 private Button mSave;
48 private Button mGoDesktop;
49 private boolean mIsOnClick;
50 private String mApName,mApPwd;
51
52 @Override
53 protected void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
55 setContentView(R.layout.activity_main);
56
57 mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
58 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
59
60 WIFI_INTENT_FILTER.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
61 registerReceiver(mReceiver, WIFI_INTENT_FILTER);
62
63 mInitView();
64 }
65
66 @Override
67 protected void onDestroy() {
68 unregisterReceiver(mReceiver);
69 super.onDestroy();
70 }
71
72 private void mInitView(){
73 root_layout = findViewById(R.id.root_layout);
74 root_layout.setOnTouchListener(this);
75
76 mAPEnable = findViewById(R.id.ap_enable);
77 mAPEnable.setText(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED?"打开热点":"关闭热点");
78
79 mEditName = findViewById(R.id.edit_name);
80 mApName = mWifiManager.getWifiApConfiguration().SSID;
81 mEditName.setText(mApName);
82
83 mEditPwd = findViewById(R.id.edit_pwd);
84 mApPwd = mWifiManager.getWifiApConfiguration().preSharedKey;
85 mEditPwd.setText(mApPwd);
86
87 mApMsg = findViewById(R.id.ap_msg);
88 mSave = findViewById(R.id.save_data);
89 mGoDesktop = findViewById(R.id.go_desktop);
90
91 mAPEnable.setOnClickListener(this);
92 mSave.setOnClickListener(this);
93 mGoDesktop.setOnClickListener(this);
94 }
95
96 private void setApName(String deviceName) {
97 final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
98 config.SSID = deviceName;
99 // TODO: If tether is running, turn off the AP and restart it after setting config.
100 mWifiManager.setWifiApConfiguration(config);
101 }
102
103 private void setApPassword(String devicePwd) {
104 final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
105 config.preSharedKey = devicePwd;
106 // TODO: If tether is running, turn off the AP and restart it after setting config.
107 mWifiManager.setWifiApConfiguration(config);
108 }
109
110 private void setApProfile(int deviceProfile) {
111 final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
112 config.allowedKeyManagement.set(deviceProfile);
113 // TODO: If tether is running, turn off the AP and restart it after setting config.
114 mWifiManager.setWifiApConfiguration(config);
115 }
116
117 private int getApProfile(){
118 final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
119 int mSecurityValue;
120 if (config != null && config.getAuthType() == WifiConfiguration.KeyMgmt.NONE) {
121 mSecurityValue = WifiConfiguration.KeyMgmt.NONE;
122 } else {
123 mSecurityValue = WifiConfiguration.KeyMgmt.WPA2_PSK;
124 }
125 return mSecurityValue;
126 }
127
128 private String getApProfileStr(int profile){
129 return profile == WifiConfiguration.KeyMgmt.NONE?"NONE":"WPA2_PSK";
130 }
131
132 private void setApEnable(boolean enable){
133 if(enable){
134 startTether();
135 }else {
136 stopTether();
137 }
138 }
139
140 private void saveConfig(){
141 if(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED){
142 runOnUiThread(new Runnable() {
143 @Override
144 public void run() {
145 Toast.makeText(MainActivity.this, "请先打开热点再保存修改!", Toast.LENGTH_LONG).show();
146 }
147 });
148 return;
149 }
150
151 if(!mApName.equals(mEditName.getText().toString())){
152 setApName(mEditName.getText().toString());
153 }
154
155 if(!mApPwd.equals(mEditPwd.getText().toString())){
156 setApPassword(mEditPwd.getText().toString());
157 }
158 }
159
160 @Override
161 public void onClick(View v) {
162 switch (v.getId()){
163 case R.id.ap_enable:
164 try {
165 setApEnable(mIsOnClick = !mIsOnClick);
166 }catch (Exception e){
167 e.printStackTrace();
168 }
169 break;
170 case R.id.save_data:
171 try {
172 saveConfig();
173 }catch (Exception e){
174 e.printStackTrace();
175 }
176 break;
177 case R.id.go_desktop:
178 moveTaskToBack(true);
179 break;
180 }
181 }
182
183 void stopTether() {
184 Log.i(TAG,"stopTether()");
185 mConnectivityManager.stopTethering(TETHERING_WIFI);
186 }
187
188 void startTether() {
189 Log.i(TAG,"startTether()");
190 mConnectivityManager.startTethering(TETHERING_WIFI, true,
191 mOnStartTetheringCallback, new Handler(Looper.getMainLooper()));
192 }
193
194 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
195 @Override
196 public void onReceive(Context context, Intent intent) {
197 String action = intent.getAction();
198 if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(action)) {
199 final int state = intent.getIntExtra(
200 WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED);
201 handleWifiApStateChanged(state);
202 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
203 }
204 }
205 };
206
207 private void handleWifiApStateChanged(int state) {
208 String changed;
209 switch (state) {
210 case WifiManager.WIFI_AP_STATE_ENABLING:
211 Log.i(TAG,"WIFI_AP_STATE_ENABLING");
212 changed = "热点开启中";
213 break;
214 case WifiManager.WIFI_AP_STATE_ENABLED:
215 Log.i(TAG,"WIFI_AP_STATE_ENABLED");
216 changed = "热点已开启";
217 break;
218 case WifiManager.WIFI_AP_STATE_DISABLING:
219 Log.i(TAG,"WIFI_AP_STATE_DISABLING");
220 changed = "热点关闭中";
221 break;
222 case WifiManager.WIFI_AP_STATE_DISABLED:
223 Log.i(TAG,"WIFI_AP_STATE_DISABLED");
224 changed = "热点已关闭";
225 break;
226 default:
227 Log.i(TAG,"default");
228 changed = "default";
229 break;
230 }
231 updateApInfo(changed);
232 }
233
234 private void updateApInfo(String changed) {
235 String name = mWifiManager.getWifiApConfiguration().SSID;
236 String pwd = mWifiManager.getWifiApConfiguration().preSharedKey;
237 String profile = getApProfileStr(getApProfile());
238
239 mUpdateMsg("stateChanged:"+changed+" name:"+name+" pwd:"+pwd+" profile:"+profile);
240 }
241
242 private void mUpdateMsg(String msg){
243 runOnUiThread(() -> {
244 mAPEnable.setText(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED?"打开热点":"关闭热点");
245 mApMsg.setText(msg);
246 });
247 }
248
249 @Override
250 public boolean onTouch(View view, MotionEvent motionEvent) {
251 if(null != getCurrentFocus()){
252 InputMethodManager manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
253 if(manager != null){
254 return manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
255 }
256 return false;
257 }
258 return true;
259 }
260 }
activity_main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 android:id="@+id/root_layout"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 android:layout_gravity="center"
9 android:gravity="center"
10 android:orientation="vertical"
11 tools:context=".MainActivity">
12
13 <Button
14 android:id="@+id/ap_enable"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:text="打开热点"/>
18
19 <TextView
20 android:id="@+id/ap_msg"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:text="热点已关闭"/>
24
25 <EditText
26 android:id="@+id/edit_name"
27 android:layout_width="200dp"
28 android:layout_height="wrap_content"
29 android:hint="热点名称"/>
30
31 <EditText
32 android:id="@+id/edit_pwd"
33 android:layout_width="200dp"
34 android:layout_height="wrap_content"
35 android:hint="热点密码"/>
36
37 <Button
38 android:id="@+id/save_data"
39 android:layout_width="wrap_content"
40 android:layout_height="wrap_content"
41 android:text="保存修改"/>
42
43 <Button
44 android:id="@+id/go_desktop"
45 android:layout_width="wrap_content"
46 android:layout_height="wrap_content"
47 android:text="回到桌面"/>
48
49 </LinearLayout>

android9.0之后wifi热点原生接口开发示例的更多相关文章

  1. 脑波设备mindwave TGCD接口开发示例

    对于TGCD的开发,神念科技提供的文件包括,头文件thinkgear.h,thinkgear.lib,thinkgear.dll,有这三个文件,在win32下开发就不是什么难事了吧 如果是java语言 ...

  2. 获取QQ状态接口开发示例

    unit checkqqstatus; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Control ...

  3. Android WiFi热点7.1以上版本适配

    代码地址如下:http://www.demodashi.com/demo/13907.html 一.准备工作 开发环境:  jdk1.8  AS(3.0.1) 运行环境:  华为V10(Android ...

  4. python之接口开发基础知识

    一.开发接口的作用 1.mock 服务:在别的接口没有开发完成的时候可以模拟一些接口以便测试已经开发完成的接口,例如假的支付接口,模拟支付成功.支付失败. 2.了解接口是如何实现的:数据交互.数据返回 ...

  5. Python flask模块接口开发学习总结

    引言 Flask 是一个简单且十分强大的Python web 框架.它被称为微框架,“微”并不是意味着把整个Web应用放入到一个Python文件,微框架中的“微”是指Flask旨在保持代码简洁且易于扩 ...

  6. 很带劲,Android9.0可以在i.MX8开发板上这样跑

    米尔MYD-JX8MX开发板移植了Android9.0操作系统,现阶段最高版本的Android9.0操作系统将给您的产品在安全与稳定性方面带来更大的提升.可惜了,这里不能上传视频在i.MX8开发板跑A ...

  7. 【移动开发】WIFI热点通信(二)

    相信大家在上一篇中已经了解了Android中WIFI热点通信的相关操作知识(http://smallwoniu.blog.51cto.com/3911954/1536126),今天我们将在上一篇代码基 ...

  8. Android 开发 创建WiFi、WiFi热点 ---开发集合

    WIFI 权限 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> < ...

  9. Android WiFi开发教程(三)——WiFi热点数据传输

    在上一篇文章中介绍了WiFi的搜索和连接,如果你还没阅读过,建议先阅读上一篇Android WiFi开发教程(二)——WiFi的搜索和连接.本篇接着简单介绍手机上如何通过WiFi热点进行数据传输. 跟 ...

  10. Android WiFi/WiFi热点开发总结

    首先看一下WiFi的自我介绍: Wi-Fi是一种允许电子设备连接到一个无线局域网(WLAN)的技术,通常使用2.4G UHF或5G SHF ISM 射频频段.连接到无线局域网通常是有密码保护的:但也可 ...

随机推荐

  1. 【SDOI2015】星际战争

    #include<cstdio> #include<queue> using namespace std; const int M = 10000; const double ...

  2. Connect-The-Dots

    Connect-The-Dots 目录 Connect-The-Dots 1 信息收集 1.1 端口扫描 1.2 后台目录扫描 1.2.1 目录分析 2 目标服务安全检测 2.1 ftp检测 2.2 ...

  3. layui 手机端时间不能滑动问题

    // 滑动问题 .laydate-time-list ol{overflow-x: hidden !important ;overflow-y: auto !important ;} //去除秒的显示 ...

  4. KingbaseES R6集群误删除备节点的集群目录恢复方式

    一.场景 一主一备环境 主节点:192.168.17.9 备节点:192.168.17.10 故障:备节点安装目录/home/kingbase/cluster备删除 二.恢复方式 1.备节点集群目录备 ...

  5. HBase架构及读写流程

    HBase架构: Client    访问HBase的接口并维护cache来加快对HBase的访问 Zookeeper    ​ 1.保证任何时候,集群中只有一个活跃master    ​ 2.存储所 ...

  6. PKU2506Tiling

    https://blog.csdn.net/Harington/article/details/86612106

  7. java学习日记20230227-dos原理

    DOS原理  磁盘操作系统 dis operating system md c:\\temp 创建文件夹 rd c:\\jyltemp 移除文件夹 相对路径和绝对路径  相对路径:从当前目录开始定位形 ...

  8. 自己写的垃圾shell

    #!/bin/bash echo -e "deb https://mirrors.aliyun.com/ubuntu/ trusty main restricted universe mul ...

  9. Voletile-多线程小例子

    public class Test{ public static volatile int t = 0; //如果没有下面的全局锁标识,则结果不一定为10*1000 public static Str ...

  10. doskey: windows版 Alias

    1.编辑doskey.bat文件 2.打开注册表寻找.HKEY_CURRENT_USER \ Software \ Microsoft \ Command Processor (自行百度) 3.添加d ...