2016-03-25 21 views

cevap

1

sizin için Etkinlik bir BroadcastReceiver kaydetmeniz gerekir, ancak izin eklemek gerekir önce ağ durumuna erişmek için ve Manifest'inizde değiştirmek: Biz BroadcastReceiver oluşturmadan önce

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 

biz bir kaç şey kurmak gerekir:

ArrayList<String> ssidList; 
WifiManager wifiManager; 
WifiScanReceiver wifiReciever; 

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

    //Wifi manager will be used to request a scan 
    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 

    //Your boradcast receiver, it will be registered with the system to be notified of SSID's available 
    wifiReciever = new WifiScanReceiver(); 

    //A list to add all the available SSID's 
    ssidList = new ArrayList<String>(); 

    //Requesting a scan, results will be returned through the BroadcastReceiver 
    wifiManager.startScan(); 
} 

Bu BoradcastReceiver

edilir

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

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

Bu tam kodudur: olacak

public class WifiActivity extends AppCompatActivity { 

    ArrayList<String> ssidList; 
    WifiManager wifiManager; 
    WifiScanReceiver wifiReciever; 

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

     //Wifi manager will be used to request a scan 
     wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 

     //Your boradcast receiver, it will be registered with the system to be notified of SSID's available 
     wifiReciever = new WifiScanReceiver(); 

     //A list to add all the available SSID's 
     ssidList = new ArrayList<String>(); 

     //Requesting a scan, results will be returned through the BroadcastReceiver 
     wifiManager.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 { 

     public void onReceive(Context c, Intent intent) { 
      List<ScanResult> wifiScanList = wifiManager.getScanResults(); 
      ssidList.clear(); // clear to make sure we do not get duplicated entries 

      for (int i = 0; i < wifiScanList.size(); i++) { 
       String ssid = wifiScanList.get(i).SSID; //Get the SSID 
       ssidList.add(ssid); //add SSID to the list 
       Log.d("SSID", ssid); 
      } 

      //You can call this to keep scaning if you need to, or you can set up a Timer to scan every X seconds 
      //wifiManager.startScan(); 
     } 
    } 
} 

Her seferinde onReceive ishal

class WifiScanReceiver extends BroadcastReceiver { 

    public void onReceive(Context c, Intent intent) { 
     List<ScanResult> wifiScanList = wifiManager.getScanResults(); 
     ssidList.clear(); // clear to make sure we do not get duplicated entries 

     for (int i = 0; i < wifiScanList.size(); i++) { 
      String ssid = wifiScanList.get(i).SSID; //Get the SSID 
      ssidList.add(ssid); //add SSID to the list 
      Log.d("SSID", ssid); 
     } 

     //You can call this to keep scaning if you need to, or you can set up a Timer to scan every X seconds 
     //wifiManager.startScan(); 
    } 
} 

Bunu çağırdığında uygulama onResume çağırdığında prefereably BroadcastReceiver kayıt ve kaydını silmek gerekir ssidList'a kaydedilecek tüm SSID'lerin bir listesini alın.

0

Bunu deneyin answer.

public class WiFiDemo extends Activity implements OnClickListener 
{  
    WifiManager wifi;  
    ListView lv; 
    TextView textStatus; 
    Button buttonScan; 
    int size = 0; 
    List<ScanResult> results; 

    String ITEM_KEY = "key"; 
    ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>(); 
    SimpleAdapter adapter; 

    /* Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     textStatus = (TextView) findViewById(R.id.textStatus); 
     buttonScan = (Button) findViewById(R.id.buttonScan); 
     buttonScan.setOnClickListener(this); 
     lv = (ListView)findViewById(R.id.list); 

     wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
     if (wifi.isWifiEnabled() == false) 
     { 
      Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show(); 
      wifi.setWifiEnabled(true); 
     } 
     this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }); 
     lv.setAdapter(this.adapter); 

     registerReceiver(new BroadcastReceiver() 
     { 
      @Override 
      public void onReceive(Context c, Intent intent) 
      { 
       results = wifi.getScanResults(); 
       size = results.size(); 
      } 
     }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));      
    } 

    public void onClick(View view) 
    { 
     arraylist.clear();   
     wifi.startScan(); 

     Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show(); 
     try 
     { 
      size = size - 1; 
      while (size >= 0) 
      { 
       HashMap<String, String> item = new HashMap<String, String>();      
       item.put(ITEM_KEY, results.get(size).SSID + " " + results.get(size).capabilities); 

       arraylist.add(item); 
       size--; 
       adapter.notifyDataSetChanged();     
      } 
     } 
     catch (Exception e) 
     { }   
    }  
} 
İlgili konular