使用Android kotlin 语言,适配Android12 版本,生成一个Fragment,实现WIFI展示列表和点击连接wifi的功能
时间: 2023-12-14 18:37:40 浏览: 128
安卓——wifi列表展示以及连接
好的,以下是实现步骤:
1. 在gradle文件中添加依赖
```kotlin
implementation 'androidx.fragment:fragment-ktx:1.3.6'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'com.google.android.material:material:1.4.0'
```
2. 创建Fragment
在项目的app/src/main/java目录下创建一个新的包名,例如:com.example.wifilist.fragment
然后在该包下创建一个新的Kotlin文件,例如:WifiListFragment.kt
在WifiListFragment.kt中编写代码如下:
```kotlin
class WifiListFragment : Fragment() {
private lateinit var wifiManager: WifiManager
private lateinit var recyclerView: RecyclerView
private lateinit var wifiListAdapter: WifiListAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_wifi_list, container, false)
recyclerView = view.findViewById(R.id.recyclerView)
wifiListAdapter = WifiListAdapter(requireContext())
recyclerView.adapter = wifiListAdapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
wifiManager = requireContext().applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (!wifiManager.isWifiEnabled) {
wifiManager.isWifiEnabled = true
}
val wifiScanReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
val wifiList = wifiManager.scanResults
wifiListAdapter.updateList(wifiList)
}
}
}
requireContext().registerReceiver(wifiScanReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
wifiManager.startScan()
}
}
```
3. 创建RecyclerView的Adapter
在项目的app/src/main/java目录下创建一个新的包名,例如:com.example.wifilist.adapter
然后在该包下创建一个新的Kotlin文件,例如:WifiListAdapter.kt
在WifiListAdapter.kt中编写代码如下:
```kotlin
class WifiListAdapter(private val context: Context) : RecyclerView.Adapter<WifiListAdapter.ViewHolder>() {
private var wifiList = listOf<ScanResult>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_wifi, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wifi = wifiList[position]
holder.ssidTextView.text = wifi.SSID
holder.signalTextView.text = "${wifi.level} dBm"
holder.itemView.setOnClickListener {
val wifiConfig = WifiConfiguration()
wifiConfig.SSID = "\"${wifi.SSID}\""
wifiConfig.preSharedKey = "\"${"password"}\""
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val netId = wifiManager.addNetwork(wifiConfig)
wifiManager.disconnect()
wifiManager.enableNetwork(netId, true)
wifiManager.reconnect()
}
}
override fun getItemCount(): Int {
return wifiList.size
}
fun updateList(wifiList: List<ScanResult>) {
this.wifiList = wifiList
notifyDataSetChanged()
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val ssidTextView: TextView = itemView.findViewById(R.id.ssidTextView)
val signalTextView: TextView = itemView.findViewById(R.id.signalTextView)
}
}
```
4. 创建WiFi列表项的布局文件
在项目的app/src/main/res/layout目录下创建一个新的布局文件,例如:item_wifi.xml
在item_wifi.xml中编写代码如下:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/ssidTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/signalTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect"
android:textSize="18sp"
android:textColor="@color/teal_200"
android:textStyle="bold" />
</LinearLayout>
```
5. 创建WiFi列表Fragment的布局文件
在项目的app/src/main/res/layout目录下创建一个新的布局文件,例如:fragment_wifi_list.xml
在fragment_wifi_list.xml中编写代码如下:
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
6. 将Fragment添加到Activity中
在项目的app/src/main/java目录下创建一个新的包名,例如:com.example.wifilist.activity
然后在该包下创建一个新的Kotlin文件,例如:MainActivity.kt
在MainActivity.kt中编写代码如下:
```kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager.beginTransaction().replace(R.id.container, WifiListFragment()).commit()
}
}
```
7. 运行程序
在模拟器或真机上运行程序,即可展示WiFi列表,并且可以点击连接WiFi。
阅读全文