Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WifiManager mainWifi;
- WifiReceiver receiverWifi;
- List<ScanResult> wifiList;
- String wifis[];
- StringBuilder sb = new StringBuilder();
- int list;
- ListView list1;
- Button b;
- ProgressDialog p;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- p=new ProgressDialog(this);
- p.setTitle("Searching Wifi Networks");
- p.setMessage("Loading");
- p.show();
- Thread t= new Thread(new updater());
- t.start();
- list1 = (ListView) findViewById(R.id.listView1);
- b=(Button)findViewById(R.id.button1);
- b.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- registerReceiver(receiverWifi, new IntentFilter(
- WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
- mainWifi.startScan();
- }
- });
- mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
- mainWifi.setWifiEnabled(true);
- receiverWifi = new WifiReceiver();
- // Register broadcast receiver
- // Broacast receiver will automatically call when number of wifi
- // connections changed
- registerReceiver(receiverWifi, new IntentFilter(
- WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
- mainWifi.startScan();
- }
- public class updater extends Thread{
- @Override
- public void run() {
- // TODO Auto-generated method stub
- super.run();
- p.incrementProgressBy(20);
- for(int ii=0;ii<5;ii++){
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }p.dismiss();
- }
- }
- 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();
- wifis = new String[wifiList.size()];
- for (int i = 0; i < wifiList.size(); i++) {
- wifis[i] = ((wifiList.get(i).SSID).toString());
- }
- //list1.setBackgroundColor(Color.BLACK);
- list1.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
- android.R.layout.simple_list_item_1 , android.R.id.text1, wifis));
- list1.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- // TODO Auto-generated method stub
- int itemPosition = position;
- // ListView Clicked item value
- String itemValue = (String) list1.getItemAtPosition(position);
- Intent i= new Intent(MainActivity.this,Progressshow.class);
- i.putExtra("Name", itemValue);
- startActivity(i);
- }
- });
- }
- }
- protected void onPause() {
- unregisterReceiver(receiverWifi);
- super.onPause();
- }
- protected void onResume() {
- registerReceiver(receiverWifi, new IntentFilter(
- WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
- super.onResume();
- }
- }
- list1.setBackgroundColor(Color.GREEN);
- android:background="@android:color/darker_gray"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement