如何在按钮点击上显示wifi网络列表

时间:2014-03-20 10:26:13

标签: android android-wifi wifimanager

我想获得点击按钮时可用的wifi网络列表 我使用的代码是这样但我无法得到结果,我得到例外...

清单文件中的

......

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

在Activity类中......

    public class MainActivity extends Activity {

    Button b;
    TextView tv;
    Context con;
WifiManager wmg;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //b=(Button)findViewById(R.id.button1);
        tv=(TextView)findViewById(R.id.textView1);
        //b.setOnClickListener(this);
        wmg = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        lv=(ListView)findViewById(R.id.listView1);
        wmg.startScan();
    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //b=(Button)findViewById(R.id.button1);
        tv=(TextView)findViewById(R.id.textView1);
        //b.setOnClickListener(this);
        wmg = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        lv=(ListView)findViewById(R.id.listView1);
        wmg.startScan();
    }
private final BroadcastReceiver myWifiScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if (intent.getAction() == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
                List<ScanResult> mScanResults = wmg.getScanResults();
                for (ScanResult result : mScanResults) {

                     Log.i("WIFI_NTWRKS",result.SSID);
                      }

                        }

            }

    };

这是例外......

logcat是......

03-20 16:28:55.008: D/AndroidRuntime(4959): Shutting down VM
03-20 16:28:55.008: W/dalvikvm(4959): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
03-20 16:28:55.008: E/AndroidRuntime(4959): FATAL EXCEPTION: main
03-20 16:28:55.008: E/AndroidRuntime(4959): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wifi/com.example.wifi.MainActivity}: java.lang.SecurityException: WifiService: Neither user 10145 nor current process has android.permission.CHANGE_WIFI_STATE.
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.os.Looper.loop(Looper.java:130)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.main(ActivityThread.java:3701)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at java.lang.reflect.Method.invoke(Method.java:507)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at dalvik.system.NativeStart.main(Native Method)
03-20 16:28:55.008: E/AndroidRuntime(4959): Caused by: java.lang.SecurityException: WifiService: Neither user 10145 nor current process has android.permission.CHANGE_WIFI_STATE.
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.os.Parcel.readException(Parcel.java:1322)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.os.Parcel.readException(Parcel.java:1276)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.net.wifi.IWifiManager$Stub$Proxy.startScan(IWifiManager.java:508)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.net.wifi.WifiManager.startScan(WifiManager.java:573)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at com.example.wifi.MainActivity.onCreate(MainActivity.java:39)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-20 16:28:55.008: E/AndroidRuntime(4959):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
03-20 16:28:55.008: E/AndroidRuntime(4959):     ... 11 more

1 个答案:

答案 0 :(得分:1)

您需要创建一个BroadcastReceiver来侦听Wifi扫描结果:

private final BroadcastReceiver myWifiScanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
    if (intent.getAction() == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
        List<ScanResult> mScanResults = wifi.getScanResults();

for (ScanResult result : mScanResults) {

 Log.i("WIFI_NTWRKS",result.SSID);
  }

    }
}
}

在onCreate()中,您将分配myWifiManager并启动扫描:

myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
myWifiManager.startScan();