在自定义列表视图中获取可用的 wi-fi 扫描结果 [英] Get available wi-fi scan result in customized listview

查看:31
本文介绍了在自定义列表视图中获取可用的 wi-fi 扫描结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在活动开始时在自定义列表视图中提供可用的 wifi 扫描结果.但我只想显示 SSID 和允许的密钥管理以及当我们检查其中的 wifi 列表时显示的与 Android 手机相同的网络强度.

I want a available wifi scan result in a customized listview on start of activity. But i want to show only SSID and Allowed key Management and the strength of network same as android phone shows when we check wifi list in it.

package com.am.wifi.amitwifi;

import java.util.List;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
       WifiManager mainWifiObj;
       WifiScanReceiver wifiReciever;
       ListView list;
       String wifis[];


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView)findViewById(R.id.listView1);
          mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
          wifiReciever = new WifiScanReceiver();
          mainWifiObj.startScan();

    }
       protected void onPause() {
              unregisterReceiver(wifiReciever);
              super.onPause();
           }

       protected void onResume() {
              registerReceiver(wifiReciever, new IntentFilter(
              WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
              super.onResume();
           }
       class WifiScanReceiver extends BroadcastReceiver {
              @SuppressLint("UseValueOf")
              public void onReceive(Context c, Intent intent) {
                 List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
                 wifis = new String[wifiScanList.size()];
                 for(int i = 0; i < wifiScanList.size(); i++){
                    wifis[i] = ((wifiScanList.get(i)).toString());
                 }

                 list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                 android.R.layout.simple_list_item_1,wifis));
              }
           }
}

推荐答案

在循环后添加以下内容

Add the following after your loop

String filtered[] = new String[wifiScanList.size()];
            int counter = 0;
            for (String eachWifi : wifis) {
                String[] temp = eachWifi.split(",");
                filtered[counter] = temp[0] +temp[2] +temp[3];//0->SSID, 2->Key Management 3-> Strength
                counter++;
            }
            list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_list_item_1, filtered));

要删除多余的单词,您可以执行以下操作:

To remove extra words you can do the following:

temp[0].substring(5).trim() 

这将删除前 5 个字符并修剪所有空格.也做类似的事情.并使用 \n 进入下一行.

This removes the first 5 characters and trims any whitespace. Do similar to others as well. And use \n to get on next line.

或者最好的主意是使用自定义适配器.查看我的回答这里

祝你好运.

这篇关于在自定义列表视图中获取可用的 wi-fi 扫描结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆