我是靠谱客的博主 仁爱大侠,这篇文章主要介绍Android 5.1系统源码Wifi模块中wifiSettings源码分析,现在分享给大家,希望可以做个参考。

在看一下代码之前需要简单了解wifi的基本知识:

802.11协议wifi用到的是802.11b,802.11g(802.11b后继标准)

station:携带无线网卡的设备,如智能手机,笔记本,底层会启动 wpa-supplicant:实现station对无线网络的管理和控制功能。

AP:accesspoint本身也是一个station,能为关联的STA提供分布式服务(ds),如路由器

DS:distributionservice:分布式服务,BSSLAN组合在一起构成一个ESS的就是ds,ds一般是指有线网络(通过它接入互联网)

BSS:BasicService Set,是由上述原件组成的网络

基础结构型BSS:通常是指的Infrastructurebasic Service Set, ap参与。

独立型BSS:通常是指IndependentBSS,不需要ap,各个sta直接互联,自组网络对等网络

           通常我们所说的BSS是指基础结构型

ESS:ExtendedService Set扩展服务集,包含一个或者多个BSS.

SSID:ServiceSet Identification:网络名

BSSID:在基础结构型网络中,他就是apMAC地址,在独立型BSS中为随机生成,

wpa-supplicant:使得无线网卡工作在managed模式,

softap:AP底层启动:hostapd的后台管理进程, 常见的为hotspot

hostapd:切换为master模式,模拟ap,建立一个无线开放的网络,


在谷歌提供的安卓源码中,网址如下:http://androidxref.com ,初学者学习,分析,留疑问,并且长期更新,修改错误,补充。

安卓的系统wifi模块,一般在设置----->Wifi中
WifiSettings显示的就是打开wifi的那个界面

需要先了解一些wifi模块的api如WifiManager类等。

WifiSettings继承SettingsPreferenceFragment,具有fragement的生命周期(可百度看一下)如sethasOptionsMenu(true)这方法是Fragment中的

这个界面一般包括

一个switchbar(控制开关,在WifiEnaber中实现),控制wifi的开关,

主要用WifiEnabler中的onSwitchChanged方法中实现

调用wifiManager的setWifiEnabled(boolean ischeck)方法进行开关

复制代码
1
preferenceScreen(用来显示ap(如路由器)列表)

OptionsMenu

选项菜单,通过sethasOptionsMenu(true)会自动调用oncreateOptionsMenu方法,

方法中调用addOptionsMenuItems进行初始

   包括新增网络,保存的网络,刷新,高级(会有条件具体显示的菜单,如通过savedNetworksExist来判断“保存的网络”是否显示在菜单上)

  ContextMenu

长按ap会弹出内容菜单,通过RegisterForContextMenu(listview),会自动调用OnCreateContextMenu方法,

包括连接,忘记,修改,写入NFC的功能(会有条件具体显示的菜单,如连接的,保存的,未连接的的ap)

 这些方法在wifiSettings中都有具体的实现代码,可以分析


WifiSettings位于packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java

其中有两个类,其中的Multimap为多重映射,在constructAccessPoints方法中会被调用

Multimap<String,AccessPoint> apMap = new Multimap<String, AccessPoint>()

apMap用来存放ssidaccesspoint的键值,其中,相同的键可以有多个值(意味着可能存在ssid相同的多个ap

负责扫描,发送消息扫描,间隔10秒,startScan(),连续三次扫描都失败就停止扫描。这个类在WifiSettings构造方法中被初始化,


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
复制代码
 private static class Scanner extends Handler {
176        private int mRetry = 0;
177        private WifiSettings mWifiSettings = null;
178
179        Scanner(WifiSettings wifiSettings) {
180            mWifiSettings = wifiSettings;
181        }
182
183        void resume() {
184            if (!hasMessages(0)) {
185                sendEmptyMessage(0);
186            }
187        }
188
189        void forceScan() {
190            removeMessages(0);
191            sendEmptyMessage(0);
192        }
193
194        void pause() {
195            mRetry = 0;
196            removeMessages(0);
197        }
198
199        @Override
200        public void handleMessage(Message message) {
201            if (mWifiSettings.mWifiManager.startScan()) {
202                mRetry = 0;                                       //当中有一次扫描成功mRetry=0;
203            } else if (++mRetry >= 3) {                         //开始扫描的操作失败mRetry+1与3比较,超过三次就return
204                mRetry = 0;
205                Activity activity = mWifiSettings.getActivity();
206                if (activity != null) {
207                    Toast.makeText(activity, R.string.wifi_fail_to_scan, Toast.LENGTH_LONG).show();
208                }
209                return;
210            }
211            sendEmptyMessageDelayed(0, WIFI_RESCAN_INTERVAL_MS); //每隔10秒,发起扫描的操作
212        }
213    }
214
复制代码
</pre><span style="font-size:10px;"><a target=_blank target="_blank" class="l" name="214" href="http://androidxref.com/5.1.0_r1/xref/packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java#214"></a></span>

在wifiSettings构造方法中,增加了Intent过滤器和广播接受者,其中广播接受者的时间在HandleEvent(Intent intent)中处理

但是我有一个问题:为啥过滤器中注册了8个Action

复制代码
复制代码
<pre name="code" class="java">       public WifiSettings() {
216        super(DISALLOW_CONFIG_WIFI);
217        mFilter = new IntentFilter();
218        mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
219        mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
220        mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
221        mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
222        mFilter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
223        mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
224        mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
225        mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
226
227        mReceiver = new BroadcastReceiver() {
228            @Override
229            public void onReceive(Context context, Intent intent) {
230                handleEvent(intent);
231            }
232        };
233
234        mScanner = new Scanner(this);
235    }


复制代码

复制代码

但是在HandleEvent方法中却只有6个action的处理,NETWORK_IDS_CHANGED_ACTION与SUPPLICANT_STATE_CHANGED_ACTION却没有处理,那么加入的目的?

HandleEvent处理广播的代码:

复制代码
复制代码
777    private void handleEvent(Intent intent) {
778        String action = intent.getAction();
779        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
780            updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
781                    WifiManager.WIFI_STATE_UNKNOWN));                           //更新wifi状态改变,Enabled Enabling Disabled
782        } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) ||
783                WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) ||
784                WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) {
785                updateAccessPoints();                                           //更新AccessPoints
786        } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
787            NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
788                    WifiManager.EXTRA_NETWORK_INFO);
789            mConnected.set(info.isConnected());
790            changeNextButtonState(info.isConnected());
791            updateAccessPoints(); 
792            updateNetworkInfo(info);                                              //更新ap再更新网络信息                                                                    
793        } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
794            updateNetworkInfo(null);
795        }
796    }
797


不同的action对应处理不同的事件:

WIFI_STATE_CHANGED_ACTION:当wifi状态改变的时候,updataWifiState(int state),根据不同的状态,做不同的处理:

复制代码
复制代码
827    private void updateWifiState(int state) {
828        Activity activity = getActivity();
829        if (activity != null) {
830            activity.invalidateOptionsMenu();
831        }
832
833        switch (state) {
834            case WifiManager.WIFI_STATE_ENABLED:
835                mScanner.resume();                                //enabled的时候,发送扫描信息startScan()
836                return; // not break, to avoid the call to pause() below  //避免调用mScanner.pause()停止扫描
837 
838            case WifiManager.WIFI_STATE_ENABLING:
839                addMessagePreference(R.string.wifi_starting);    //加入“正在打开wifi”
840                break;
841
842            case WifiManager.WIFI_STATE_DISABLED:
843                setOffMessage();                                //wifi不可用的时候,显示一些其他信息(通过provider判断)
844                break;
845        }
846
847        mLastInfo = null;
848        mLastNetworkInfo = null;
849        mScanner.pause();                                      //停止扫描
850    }

第二个的判断条件中有三个action:分别对应的是

SCAN_RESULTS_AVAILABLE_ACTION:

An access point scan has completed, and results are available from the supplicant.

一个AP扫描完成,并且从supplicant获得的结果是可用的

CONFIGURED_NETWORKS_CHANGED_ACTION:

Broadcast intent action indicating that the configured networks changed. This can be as a result of adding/updating/deleting a network

广播intent的动作表明配置的网络已经改变,比如增加/更新/删除一个网络

LINK_CONFIGURATION_CHANGED_ACTION:

Broadcast intent action indicating that the link configuration changed on wifi

广播intent的动作表明连接的配置已经改变

在这些情况下,都会更新wifi的信息updateAccessPoints();

复制代码
复制代码
640    private void updateAccessPoints() {
641        // Safeguard from some delayed event handling
642        if (getActivity() == null) return;
643
644        if (isUiRestricted()) 
645            addMessagePreference(R.string.wifi_empty_list_user_restricted);     //判断是否有限制
646            return;
647        }
648        final int wifiState = mWifiManager.getWifiState();
649
650        //when we update the screen, check if verbose logging has been turned on or off
651        mVerboseLogging = mWifiManager.getVerboseLoggingLevel();
652
653        switch (wifiState) {                                                  //根据wifi状态来处理
654            case WifiManager.WIFI_STATE_ENABLED:                              //当wifi状态可用的情况下
655                // AccessPoints are automatically sorted with TreeSet.
656                final Collection<AccessPoint> accessPoints =
657                        constructAccessPoints(getActivity(), mWifiManager, mLastInfo,
658                                mLastNetworkInfo);                           //主要通过constructAccessPoints进行更新
659                getPreferenceScreen().removeAll();
660                if (accessPoints.size() == 0) {
661                    addMessagePreference(R.string.wifi_empty_list_wifi_on); //如果ap没有,则显示“正在搜索wlan网络”
662                }
663
664                for (AccessPoint accessPoint : accessPoints) {
665                    // Ignore access points that are out of range.
666                    if (accessPoint.getLevel() != -1) {
667                        getPreferenceScreen().addPreference(accessPoint);  //遍历,增加到preferenceScreen中(可以阅读相关资料了解)
668                    }
669                }
670                break;
671
672            case WifiManager.WIFI_STATE_ENABLING:
673                getPreferenceScreen().removeAll();                         //enabling的情况下,移出preferenceScreen中所有的ap
674                break;
675
676            case WifiManager.WIFI_STATE_DISABLING:
677                addMessagePreference(R.string.wifi_stopping);             //显示“正在关闭“                   
678                break;
679
680            case WifiManager.WIFI_STATE_DISABLED:                        //不可用的时候,显示其他信息
681                setOffMessage();
682                break;
683        }
684    }
685

这里面最主要的方法就是constructAccessPoints这个方法了,之后在学习把。。NETWORK_STATE_CHANGED_ACTIONBroadcast intent action indicating that the state of Wi-Fi connectivity has changed. One extra provides the new statewifi连通性被改变,提供了新的状态更新ap的同时,更新NetworkInfo,在安卓5.0的情况下名字为updateConnectionState(估计认为这是网络状态?怕理解成connected的ap???)
复制代码

复制代码
798    private void updateNetworkInfo(NetworkInfo networkInfo) {
799        /* sticky broadcasts can call this when wifi is disabled */
800        if (!mWifiManager.isWifiEnabled()) {                                                   
801            mScanner.pause();                                                        
802            return;
803        }
804
805        if (networkInfo != null &&
806                networkInfo.getDetailedState() == DetailedState.OBTAINING_IPADDR) {
807            mScanner.pause();
808        } else {
809            mScanner.resume();
810        }
811
812        mLastInfo = mWifiManager.getConnectionInfo();
813        if (networkInfo != null) {
814            mLastNetworkInfo = networkInfo;
815        }
816                                                                      //倒序更新AccessPoint的信息,应该是更新了修改配置之后的ap的信息
817        for (int i = getPreferenceScreen().getPreferenceCount() - 1; i >= 0; --i) {
818            // Maybe there's a WifiConfigPreference
819            Preference preference = getPreferenceScreen().getPreference(i);
820            if (preference instanceof AccessPoint) {
821                final AccessPoint accessPoint = (AccessPoint) preference;
822                accessPoint.update(mLastInfo, mLastNetworkInfo);                      
823            }
824        }
825    }


RSSI_CHANGED_ACTION
The RSSI (signal strength) has changed.
显而易见,这指的是信号强度被改变的action,调用updateNetworkInfo,更新一下网络信息就可以了
以上是handleEvent的

=============================================handleEvent分割线======================================================================
复制代码
之前看到在updataAccessPoints中调用了这个方法:constructAccessPoints,看一下这个方法的源码把
复制代码
复制代码
private static List<AccessPoint> constructAccessPoints(Context context,
719            WifiManager wifiManager, WifiInfo lastInfo, NetworkInfo lastNetworkInfo) {
720        ArrayList<AccessPoint> accessPoints = new ArrayList<AccessPoint>();       //存放ap的ArrayList,用来返回
721        /** Lookup table to more quickly update AccessPoints by only considering objects with the
722         * correct SSID.  Maps SSID -> List of AccessPoints with the given SSID.  */
723        Multimap<String, AccessPoint> apMap = new Multimap<String, AccessPoint>();//多重映射,键SSID(网络名)--值(ap)
724
725        final List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();//获取手机中保存过配置的连接信息
726        if (configs != null) {
727            // Update "Saved Networks" menu option.                //更新选项“saved Networks”的状态
728            if (savedNetworksExist != (configs.size() > 0)) {      //比较式前后一致的返回值,更新Saved Networks的菜单选项
                                                                      //例如:没有存储的wifi信息,那么savedNetworksExist为flase
                                                                      //例如:有存储的wifi信息,那么savedNetworksExist为true
729                savedNetworksExist = !savedNetworksExist;
730                if (context instanceof Activity) {
731                    ((Activity) context).invalidateOptionsMenu();   //刷新optionMenu
732                }
733            }
734            for (WifiConfiguration config : configs) {                   //对配置过的信息进行遍历
735                if (config.selfAdded && config.numAssociation == 0) {    // Number of time we associated to this configuration
736                    continue;                                            //跳过本次循环   
737                }
738                AccessPoint accessPoint = new AccessPoint(context, config);
739                if (lastInfo != null && lastNetworkInfo != null) {
740                    accessPoint.update(lastInfo, lastNetworkInfo);      
741                }
742                accessPoints.add(accessPoint);                          //把ap加入List中
743                apMap.put(accessPoint.ssid, accessPoint);
744            }
745        }
746
747        final List<ScanResult> results = wifiManager.getScanResults();   //wifi扫描结果的的处理
748        if (results != null) {
749            for (ScanResult result : results) {                          //遍历扫描结果
750                // Ignore hidden and ad-hoc networks.                    //忽略隐藏的(没有SSID)以及ad-hoc(IBSS?)
751                if (result.SSID == null || result.SSID.length() == 0 ||
752                        result.capabilities.contains("[IBSS]")) {
753                    continue;
754                }
755
756                boolean found = false;                                  //第一次的apMap中存放了配置过的信息
757                for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {
758                    if (accessPoint.update(result))                     //判断扫描结果ssid和security安全协议是否存在过,存在更新
759                        found = true;                                 
760                }
761                if (!found) {                                            //未找到的情况下,加入accesspoints的
762                    AccessPoint accessPoint = new AccessPoint(context, result);
763                    if (lastInfo != null && lastNetworkInfo != null) {
764                        accessPoint.update(lastInfo, lastNetworkInfo);
765                    }
766                    accessPoints.add(accessPoint);
767                    apMap.put(accessPoint.ssid, accessPoint);           //放入apMap,再次遍历会调用
768                }
769            }
770        }
771
772        // Pre-sort accessPoints to speed preference insertion
773        Collections.sort(accessPoints);
774        return accessPoints;
775    }

==============================================================================================================
复制代码
来了解一下所有的重写的方法:OptionsMenu:调用addOptionsMenuItems方法进行初始化
复制代码
复制代码
397    void addOptionsMenuItems(Menu menu) {       //这里的通过wifi是否打开的状态设置menu中元素的enable状态
398        final boolean wifiIsEnabled = mWifiManager.isWifiEnabled();
399        TypedArray ta = getActivity().getTheme().obtainStyledAttributes(
400                new int[] {R.attr.ic_menu_add, R.attr.ic_wps});
401        menu.add(Menu.NONE, MENU_ID_ADD_NETWORK, 0, R.string.wifi_add_network)
402                .setIcon(ta.getDrawable(0))
403                .setEnabled(wifiIsEnabled)          //增加网络,当wifi不可用的时候为false,不能点击
404                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
405        if (savedNetworksExist) {
406            menu.add(Menu.NONE, MENU_ID_SAVED_NETWORK, 0, R.string.wifi_saved_access_points_label)
407                    .setIcon(ta.getDrawable(0))
408                    .setEnabled(wifiIsEnabled)       //通过判断是否存在保存的网络,来决定显示与否
409                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
410        }
411        menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.menu_stats_refresh)
412               .setEnabled(wifiIsEnabled)
413               .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);//刷新,通过判断wifi状态来决定是否能刷新
414        menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced)
415                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); //高级选项
416        ta.recycle();
417    
OptionMenuItems对应的点击事件为:onOptionsItemSelected,根据不同的item点开对应的dialog,
当然,onCrateContextMenu中对应的点击事件为onContextItemSelected,根据不同的选项执行不同的操作
当然还有一些重写的方法,那就是生命周期的重写:如:
先执行构造方法,然后方法如下:

重写onActivityCreated(参数)

1.获得系统服务WifiManager

2.注册监听,connect,save,forget

3.savedInstanceState的状态判断,进行一些初始化


复制代码
<pre name="code" class="java">@Override
238    public void onActivityCreated(Bundle savedInstanceState) {
239        super.onActivityCreated(savedInstanceState);
240        //获得服务
241        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
242        //三种监听
243        mConnectListener = new WifiManager.ActionListener() {
244                                   @Override
245                                   public void onSuccess() {
246                                   }
247                                   @Override
248                                   public void onFailure(int reason) {
249                                       Activity activity = getActivity();
250                                       if (activity != null) {
251                                           Toast.makeText(activity,
252                                                R.string.wifi_failed_connect_message,
253                                                Toast.LENGTH_SHORT).show();
254                                       }
255                                   }
256                               };
257
258        mSaveListener = new WifiManager.ActionListener() {
259                                @Override
260                                public void onSuccess() {
261                                }
262                                @Override
263                                public void onFailure(int reason) {
264                                    Activity activity = getActivity();
265                                    if (activity != null) {
266                                        Toast.makeText(activity,
267                                            R.string.wifi_failed_save_message,
268                                            Toast.LENGTH_SHORT).show();
269                                    }
270                                }
271                            };
272
273        mForgetListener = new WifiManager.ActionListener() {
274                                   @Override
275                                   public void onSuccess() {
276                                   }
277                                   @Override
278                                   public void onFailure(int reason) {
279                                       Activity activity = getActivity();
280                                       if (activity != null) {
281                                           Toast.makeText(activity,
282                                               R.string.wifi_failed_forget_message,
283                                               Toast.LENGTH_SHORT).show();
284                                       }
285                                   }
286                               };
287        
288        if (savedInstanceState != null) {
289            mDlgEdit = savedInstanceState.getBoolean(SAVE_DIALOG_EDIT_MODE);
290            if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {
291                mAccessPointSavedState =
292                    savedInstanceState.getBundle(SAVE_DIALOG_ACCESS_POINT_STATE);
293            }
294        }
295
296        // if we're supposed to enable/disable the Next button based on our current connection
297        // state, start it off in the right state
298        Intent intent = getActivity().getIntent();
299        mEnableNextOnConnection = intent.getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false);
300
301        if (mEnableNextOnConnection) {
302            if (hasNextButton()) {
303                final ConnectivityManager connectivity = (ConnectivityManager)
304                        getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
305                if (connectivity != null) {
306                    NetworkInfo info = connectivity.getNetworkInfo(
307                            ConnectivityManager.TYPE_WIFI);
308                    changeNextButtonState(info.isConnected());
309                }
310            }
311        }
312
313        addPreferencesFromResource(R.xml.wifi_settings);
314          
315        mEmptyView = initEmptyView();                     
316              registerForContextMenu(getListView()); //这样会调用onCreateContextMenu(ContextMenu, View, ContextMenuInfo) 的方法
317        setHasOptionsMenu(true);                     //会调用onCreateOptionsMenu
318        
319        if (intent.hasExtra(EXTRA_START_CONNECT_SSID)) {
320            String ssid = intent.getStringExtra(EXTRA_START_CONNECT_SSID);
321            updateAccessPoints();
322            PreferenceScreen preferenceScreen = getPreferenceScreen();
323            for (int i = 0; i < preferenceScreen.getPreferenceCount(); i++) {
324                Preference preference = preferenceScreen.getPreference(i);
325                if (preference instanceof AccessPoint) {
326                    AccessPoint accessPoint = (AccessPoint) preference;
327                    if (ssid.equals(accessPoint.ssid) && accessPoint.networkId == -1
328                            && accessPoint.security != AccessPoint.SECURITY_NONE) {
329                        onPreferenceTreeClick(preferenceScreen, preference);
330                        break;
331                    }
332                }
333            }
334        }
335    }
336


复制代码
1
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
复制代码

重写onstart(参数)

WifiEnabler进行创建对象:主要是对switchbar进行操作

wifiEnabler的构造方法中,加入intent-filter来接受广播


复制代码
346    @Override
347    public void onStart() {
348        super.onStart();
349
350        // On/off switch is hidden for Setup Wizard (returns null)
351        mWifiEnabler = createWifiEnabler();
352    }


重写onResume(参数)

做了三件事情:1加入switchbar的广播注册,加入switch的监听

2.WifiSettings注册广播

3更新ap

复制代码
362    @Override
363    public void onResume() {
364        final Activity activity = getActivity();
365        super.onResume();
366        if (mWifiEnabler != null) {
367            mWifiEnabler.resume(activity);
368        }
369
370        activity.registerReceiver(mReceiver, mFilter);
371        updateAccessPoints();
372    }


重写onPause(参数)

做了三件事:1wifiEnabler中解除广播注册,移出switch监听

2.WifiSettings移出广播

3.停止扫描移出scanner中的message

复制代码
374    @Override
375    public void onPause() {
376        super.onPause();
377        if (mWifiEnabler != null) {
378            mWifiEnabler.pause();
379        }
380
381        getActivity().unregisterReceiver(mReceiver);
382        mScanner.pause();
383    }



重写onDestroyView(参数):

1.隐藏switchbar:teardownSwitchbar()

复制代码
337    @Override
338    public void onDestroyView() {
339        super.onDestroyView();
340
341        if (mWifiEnabler != null) {
342            mWifiEnabler.teardownSwitchBar();
343        }
344    }


复制代码

复制代码
1
复制代码
1
复制代码
1

最后

以上就是仁爱大侠最近收集整理的关于Android 5.1系统源码Wifi模块中wifiSettings源码分析的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部