搜索Wifi网络

时间:2017-10-06 19:23:02

标签: java android wifi

我正在尝试搜索可用的无线网络,但没有任何反应

这是我的代码

执行我的代码后没有发生错误。

我刚开始使用BroadcastReceiver,我认为注册接收器时存在问题。

public class MainActivity extends AppCompatActivity {

public class WifiReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        wifilist = wifi.getScanResults();

        txt.setText(wifilist.get(0).toString());

    }
}

WifiManager wifi;
WifiReceiver receiver;
List<ScanResult> wifilist;

String result = "";

TextView txt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt = (TextView) findViewById(R.id.txt) ;

    wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    if(!wifi.isWifiEnabled())
    {
        Toast.makeText(getApplicationContext(), "Wifi is Disabaled .. enabled it", Toast.LENGTH_LONG ).show();
        boolean b = wifi.setWifiEnabled(true);
    }



    receiver = new WifiReceiver();
    registerReceiver(receiver, new 
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();


}

@Override
protected void onResume() {

    registerReceiver(receiver, new 
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

    super.onResume();
}

@Override
protected void onPause() {

    unregisterReceiver(receiver);

    super.onPause();
}


}

我将名为txt的TextView显示为第一个网络,但它的名称不会改变

0 个答案:

没有答案