欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?kotlin?跳轉(zhuǎn)手機(jī)熱點(diǎn)開關(guān)頁面和判斷熱點(diǎn)是否打開(親測可用)

 更新時(shí)間:2023年08月22日 16:32:26   作者:我懷里的貓  
跳轉(zhuǎn)手機(jī)熱點(diǎn)的頁面肯定是用intent,重點(diǎn)是action不知道是什么,網(wǎng)上最常見的就是Settings.ACTION_WIFI_SETTINGS 跳轉(zhuǎn)wifi設(shè)置頁面,本文介紹Android?kotlin?跳轉(zhuǎn)手機(jī)熱點(diǎn)開關(guān)頁面和判斷熱點(diǎn)是否打開,感興趣的朋友一起看看吧

判斷熱點(diǎn)是否打開

網(wǎng)上方法比較多,我這邊使用了通過WifiManager 拿反射的getWifiApState方法獲取判斷,親測可用,最近迷上了擴(kuò)展方法【dog】

fun Activity.isWifiApOpen(): Boolean {
    try {
        val manager = this.getSystemService(Context.WIFI_SERVICE) as WifiManager
        //通過放射獲取 getWifiApState()方法
        val method = manager.javaClass.getDeclaredMethod("getWifiApState")
        //調(diào)用getWifiApState() ,獲取返回值
        val state = method.invoke(manager) as Int
        //通過放射獲取 WIFI_AP的開啟狀態(tài)屬性
        val field: Field = manager.javaClass.getDeclaredField("WIFI_AP_STATE_ENABLED")
        //獲取屬性值
        val value = field.get(manager) as Int
        //判斷是否開啟
        return state == value
    } catch (e: NoSuchMethodException) {
        e.printStackTrace()
    } catch (e: IllegalAccessException) {
        e.printStackTrace()
    } catch (e: InvocationTargetException) {
        e.printStackTrace()
    } catch (e: NoSuchFieldException) {
        e.printStackTrace()
    }
    return false
}

跳轉(zhuǎn)手機(jī)熱點(diǎn)開關(guān)頁面

跳轉(zhuǎn)手機(jī)熱點(diǎn)的頁面肯定是用intent,重點(diǎn)是action不知道是什么,網(wǎng)上最常見的就是Settings.ACTION_WIFI_SETTINGS 跳轉(zhuǎn)wifi設(shè)置頁面,費(fèi)了一番力氣,同樣寫為擴(kuò)展方法,我試過是可以跳轉(zhuǎn)熱點(diǎn)設(shè)置頁面的

值得注意的是,在部分機(jī)型或特殊情況下可能會沒有這個(gè)activity,因?yàn)閒ramework層可能更改名稱,所以要做好準(zhǔn)備

fun Activity.startWifiActivity(){
    val intent = Intent(Settings.ACTION_WIRELESS_SETTINGS)
    this.startActivity(intent)
}

順帶介紹一些其他常用的設(shè)置頁面跳轉(zhuǎn)

同樣在部分機(jī)型或特殊情況下可能會沒有這個(gè)activity,得自己試試

ACTION_SETTINGS顧名思義 跳轉(zhuǎn)到系統(tǒng)設(shè)置頁面

ACTION_APN_SETTINGS跳轉(zhuǎn) APN設(shè)置界面

ACTION_LOCATION_SOURCE_SETTINGS跳轉(zhuǎn)位置服務(wù)界面(管理已安裝的應(yīng)用程序)

ACTION_AIRPLANE_MODE_SETTINGS跳轉(zhuǎn)飛行模式設(shè)置頁面

ACTION_APPLICATION_DEVELOPMENT_SETTINGS跳轉(zhuǎn)開發(fā)者選項(xiàng)頁面

ACTION_APPLICATION_SETTINGS跳轉(zhuǎn)應(yīng)用程序列表界面

ACTION_BLUETOOTH_SETTINGS跳轉(zhuǎn)藍(lán)牙設(shè)置頁面

其他熱點(diǎn)的一些相關(guān)知識

如果你的熱點(diǎn)不需要聯(lián)網(wǎng),可以使用Local-only hotspot

Local-only hotspot

Local-only hotspot 通過配置local-only熱點(diǎn),連接到Wi-Fi熱點(diǎn)的設(shè)備上的應(yīng)用可以相互通信。用這種方法創(chuàng)建的網(wǎng)絡(luò)將不能訪問Internet。每個(gè)應(yīng)用程序只能對熱點(diǎn)進(jìn)行一次請求,但多個(gè)應(yīng)用程序可以同時(shí)請求熱點(diǎn)。當(dāng)多個(gè)應(yīng)用程序成功并發(fā)注冊時(shí),它們共享底層熱點(diǎn)。onstarted (localonlyhotspotrevation)在熱點(diǎn)準(zhǔn)備就緒時(shí)被調(diào)用。

如果我們的應(yīng)用程序針對Android 13 (API級別33)或更高版本,則必須請求NEARBY_WIFI_DEVICES使用僅限本地的熱點(diǎn),如下面的代碼片段所示。針對較早版本Android的應(yīng)用必須請求ACCESS_FINE_LOCATION。

<manifest ...>
    <<!-- If your app targets Android 13 (API level 33)
          or higher, you must declare the NEARBY_WIFI_DEVICES permission. -->
    <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"
                     <!-- If your app derives location information from
                          Wi-Fi APIs, don't include the "usesPermissionFlags"
                          attribute. -->
                     android:usesPermissionFlags="neverForLocation" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
                     <!-- If any feature in your app relies on
                          precise location information, don't include the
                          "maxSdkVersion" attribute. -->
                     android:maxSdkVersion="32" />
    <application ...>
        ...
    </application>
</manifest>

對應(yīng)方法

WifiManager中的

public void startLocalOnlyHotspot (WifiManager.LocalOnlyHotspotCallback callback, 
                Handler handler)

可以同時(shí)連多個(gè),開啟失敗在callback中會有回調(diào)

參考

安卓官網(wǎng)WifiManager

到此這篇關(guān)于Android kotlin 跳轉(zhuǎn)手機(jī)熱點(diǎn)開關(guān)頁面和判斷熱點(diǎn)是否打開的文章就介紹到這了,更多相關(guān)Android kotlin 跳轉(zhuǎn)手機(jī)熱點(diǎn)開關(guān)頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論