日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 8.0一下热点启动

發布時間:2023/12/20 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 8.0一下热点启动 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近需要實現熱點的自動,但是各種方式之后只能實現Android 8.0以下的穩定實現;

/*** 自動開啟熱點工具,僅對Android 8以下有效* <p>* 需要以下權限才能正常使用* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>* <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>* <uses-permission android:name="android.permission.WRITE_SETTINGS"/>** @author jiangjunjie01* Date: 2022/3/18*/ public class HotPointHelper {/*** 開啟便攜熱點*/public static boolean openAp(Context context) {if (context == null) {return false;}if (isApOn(context)) {return false;}if (!checkPermission(context)) {return false;}WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);if (wifimanager == null) {return false;}//開啟熱點需要關閉wifiif (wifimanager.isWifiEnabled()) {wifimanager.setWifiEnabled(false);}//需要設置一個自定義熱點,不能直接使用 new WifiConfiguration() ,必須有配置信息WifiConfiguration wifiConfiguration = createConfig("1", "1");//使用反射開啟Wi-Fi熱點Method method;try {method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);method.invoke(wifimanager, wifiConfiguration, true);return true;} catch (Exception e) {e.printStackTrace();}return false;}/*** 檢查是否允許修改系統設置,需要 WRITE_SETTINGS 權限,無權限則跳轉設置頁面*/private static boolean checkPermission(Context context) {//安卓6以上的特殊處理if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (!Settings.System.canWrite(context)) {context.startActivity(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS));return false;}}return true;}/*** 檢測是否打開了熱點*/private static boolean isApOn(Context context) {WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE);if (wifimanager != null) {Method method = null;try {method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled");method.setAccessible(true);return (boolean) method.invoke(wifimanager);} catch (Exception e) {e.printStackTrace();}}return false;}/*** 關閉便攜熱點*/private static void closeAp(Application context) {try {WifiManager wifimanager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);method.invoke(wifimanager, null, false);} catch (Exception e) {e.printStackTrace();}}/*** 設置有密碼的熱點信息*/private static WifiConfiguration createConfig(String SSID, String pwd) {if (TextUtils.isEmpty(pwd)) {return null;}WifiConfiguration config = new WifiConfiguration();config.SSID = SSID;config.preSharedKey = pwd;config.status = WifiConfiguration.Status.ENABLED;config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);return config;} }

總結

以上是生活随笔為你收集整理的Android 8.0一下热点启动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。