2015-10-07 78 views
1
public class MainActivity extends AppCompatActivity { 

WifiManager wifi; 
WifiReceiver wifiReceiver; 
StringBuilder sb = new StringBuilder(); 
TextView t ; 
List<ScanResult> wifiList; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 
    wifi.startScan(); 
    t=(TextView) findViewById(R.id.text); 
    // t.setText("Scanning...."); 
} 


class WifiReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
      wifiList = wifi.getScanResults(); 
      sb.append("\nNumber Of Wifi connections :"+wifiList.size()+"\n\n"); 
      for(int i = 0; i < wifiList.size(); i++){ 

       sb.append(new Integer(i+1).toString() + ". "); 
       sb.append((wifiList.get(i)).toString()); 
       sb.append("\n\n"); 
      } 
     t.setText(sb); 
    } 
} 
} 

我想在列表中顯示wifi網絡。我編寫了這段代碼,但屏幕上沒有顯示任何內容。有人能指出我出錯的地方嗎?無法在Android應用程序中找到WiFi網絡

謝謝!

回答

1

你錯過了一件事,你需要初始化WifiReceiver參考:

wifiReceiver = new WifiReceiver(); 

我只是這種變化進行了測試,它爲我工作。

我把它用在一個片段:

public class BlankFragment extends Fragment { 

    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    private String mParam1; 
    private String mParam2; 

    WifiManager wifi; 
    WifiReceiver wifiReceiver; 
    StringBuilder sb = new StringBuilder(); 
    TextView t ; 
    List<ScanResult> wifiList; 

    public static BlankFragment newInstance(String param1) { 
     BlankFragment fragment = new BlankFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public BlankFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 

     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView = inflater.inflate(R.layout.fragment_blank, container, false); 

     wifiReceiver = new WifiReceiver(); //added 

     wifi = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE); 
     getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 
     wifi.startScan(); 
     t=(TextView) rootView.findViewById(R.id.text); 
     return rootView; 
    } 

    class WifiReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      wifiList = wifi.getScanResults(); 
      sb.append("\nNumber Of Wifi connections :"+wifiList.size()+"\n\n"); 
      for(int i = 0; i < wifiList.size(); i++){ 

       sb.append(new Integer(i+1).toString() + ". "); 
       sb.append((wifiList.get(i)).toString()); 
       sb.append("\n\n"); 
      } 
      t.setText(sb); 
     } 
    } 

} 

一定要設置正確的權限,以及:

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

結果:

enter image description here

+0

由於它的工作! – Ravi

相關問題