将wifi扫描结果显示为可点击按钮

时间:2017-01-18 18:35:07

标签: java android android-adapter

如何通过点击我连接到wifi网络来创建一个将wifi结果显示为可点击按钮或其他内容的应用程序?

以下代码将SSID显示为List但不可点击。

    package com.example.amit.ak504;
    import java.util.List;
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.wifi.ScanResult;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {
        private static int LENGTH_SHORT=600;
        TextView mainText;
        WifiManager mainWifi;
        WifiReceiver receiverWifi;
        List<ScanResult> wifiList;// this is an object of List
        StringBuilder sb = new StringBuilder();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  // setupActionBar();
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    mainText = (TextView) findViewById(R.id.tv1);
    mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    if (mainWifi.isWifiEnabled() == false)
    {
        // If wifi disabled then enable it
        Toast.makeText(getApplicationContext(), "wifi is disabled..making it  
     enabled",Toast.LENGTH_LONG).show();

        mainWifi.setWifiEnabled(true);
    }
    receiverWifi = new WifiReceiver();
    registerReceiver(receiverWifi, new
  IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    mainWifi.startScan();
    mainText.setText("Starting Scan...");
 }
 //  private void setupActionBar() {

//    if (actionBar != null) {
        // Show the Up button in the action bar.
  //      actionBar.setDisplayHomeAsUpEnabled(true);
//    }
//}
public ActionBar getSupportActionBar() {
    return getDelegate().getSupportActionBar();
}
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 0, 0, "Refresh");//side menu
    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
    mainWifi.startScan();
    mainText.setText("Starting Scan");
    return super.onOptionsItemSelected(item);
}

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

protected void onResume() {
    registerReceiver(receiverWifi, new   
      IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
}

// Broadcast receiver class called its receive method
// when number of wifi connections changed

class WifiReceiver extends BroadcastReceiver {



    // This method call when number of wifi connections changed
    public void onReceive(Context c, Intent intent)
    {
        sb = new StringBuilder();
        wifiList = mainWifi.getScanResults();
        //why getScanResult is stored into a list.
        //note that wifilist was a object of List<ScanResult>
        sb.append("\nNumber Of Wifi connections :"+wifiList.size()+"\n\n");
 Button bt1=(Button)findViewById(R.id.button);

        for(int i = 0; i < wifiList.size(); i++){


            sb.append(new Integer(i+1).toString() + ". ");
            sb.append((wifiList.get(i)).SSID);
            sb.append("\n\n");
        }
bt1.setText(wifiList.get(2).SSID);
        mainText.setText(sb);
    }

}
}

这就是我想要实现的目标:

enter image description here

1 个答案:

答案 0 :(得分:0)

使用回收站视图和适配器。在你的适配器中,听一下itens中的点击。

如何使用recyclerview:

https://www.raywenderlich.com/126528/android-recyclerview-tutorial