Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. WifiManager mainWifi;
  2. WifiReceiver receiverWifi;
  3. List<ScanResult> wifiList;
  4. String wifis[];
  5. StringBuilder sb = new StringBuilder();
  6. int list;
  7. ListView list1;
  8. Button b;
  9. ProgressDialog p;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15.  
  16.  
  17.  
  18. p=new ProgressDialog(this);
  19. p.setTitle("Searching Wifi Networks");
  20. p.setMessage("Loading");
  21. p.show();
  22.  
  23. Thread t= new Thread(new updater());
  24. t.start();
  25.  
  26. list1 = (ListView) findViewById(R.id.listView1);
  27. b=(Button)findViewById(R.id.button1);
  28. b.setOnClickListener(new OnClickListener() {
  29.  
  30. @Override
  31. public void onClick(View v) {
  32. // TODO Auto-generated method stub
  33. registerReceiver(receiverWifi, new IntentFilter(
  34. WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  35. mainWifi.startScan();
  36.  
  37. }
  38. });
  39. mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  40. mainWifi.setWifiEnabled(true);
  41. receiverWifi = new WifiReceiver();
  42.  
  43.  
  44. // Register broadcast receiver
  45. // Broacast receiver will automatically call when number of wifi
  46. // connections changed
  47. registerReceiver(receiverWifi, new IntentFilter(
  48. WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  49. mainWifi.startScan();
  50. }
  51.  
  52. public class updater extends Thread{
  53. @Override
  54. public void run() {
  55.  
  56. // TODO Auto-generated method stub
  57. super.run();
  58. p.incrementProgressBy(20);
  59. for(int ii=0;ii<5;ii++){
  60. try {
  61. Thread.sleep(500);
  62. } catch (InterruptedException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. }
  66. }p.dismiss();
  67. }
  68. }
  69.  
  70. class WifiReceiver extends BroadcastReceiver {
  71.  
  72. // This method call when number of wifi connections changed
  73. public void onReceive(Context c, Intent intent) {
  74.  
  75. sb = new StringBuilder();
  76. wifiList = mainWifi.getScanResults();
  77. wifis = new String[wifiList.size()];
  78. for (int i = 0; i < wifiList.size(); i++) {
  79. wifis[i] = ((wifiList.get(i).SSID).toString());
  80. }
  81. //list1.setBackgroundColor(Color.BLACK);
  82.  
  83.  
  84.  
  85.  
  86. list1.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
  87. android.R.layout.simple_list_item_1 , android.R.id.text1, wifis));
  88.  
  89. list1.setOnItemClickListener(new OnItemClickListener() {
  90.  
  91. @Override
  92. public void onItemClick(AdapterView<?> parent, View view,
  93. int position, long id) {
  94. // TODO Auto-generated method stub
  95. int itemPosition = position;
  96.  
  97. // ListView Clicked item value
  98. String itemValue = (String) list1.getItemAtPosition(position);
  99.  
  100. Intent i= new Intent(MainActivity.this,Progressshow.class);
  101. i.putExtra("Name", itemValue);
  102. startActivity(i);
  103. }
  104.  
  105.  
  106. });
  107.  
  108. }
  109. }
  110.  
  111.  
  112.  
  113.  
  114. protected void onPause() {
  115. unregisterReceiver(receiverWifi);
  116. super.onPause();
  117. }
  118.  
  119. protected void onResume() {
  120. registerReceiver(receiverWifi, new IntentFilter(
  121. WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  122. super.onResume();
  123. }
  124. }
  125.  
  126. list1.setBackgroundColor(Color.GREEN);
  127.  
  128. android:background="@android:color/darker_gray"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement