问题描述:
android平板状态栏整合到了屏幕右下角, 我们在使用某些WIFI模块的时候会发现, WIFI热点扫描后, 可以看到热点有信号显示, 但是, 在连接上热点后, 右下角却始终没有信号格显示。
原因分析:
在WIFI连接后, 状态栏一直接收不到信号值变化的广播, 导致无法获取到WIFI信号值。至于为何收不到, 应该就跟模块和驱动有关了。
frameworks/base/packages/apps/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) { mWifiRssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200); mWifiLevel = WifiManager.calculateSignalLevel( mWifiRssi, WifiIcons.WIFI_LEVEL_COUNT); }
解决办法:
既然在扫描热点中有信号, 那么可以通过获取此信号来显示。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.anson.wifirssi" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <!-- activity android:name="com.anson.wifirssi.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity--> <receiver android:name="com.anson.wifirssi.BootReceiver"> <intent-filter android:priority="100"> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver> <service android:name="com.anson.wifirssi.RSSIService"></service> </application> </manifest>
package com.anson.wifirssi; import java.util.List; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.NetworkInfo; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Handler; import android.os.IBinder; import android.view.Menu; import android.widget.TextView; import android.widget.Toast; public class RSSIService extends Service { WifiManager wifiMgr = null; BroadcastReceiver receiver; String connectedSSID =""; int time = 0; boolean mWifiConnected; Handler h = new Handler(){ public void handleMessage(android.os.Message msg) { showWifiState(); h.sendEmptyMessageDelayed(0, 2000); if(time == 5){ wifiMgr.startScan(); time = 0; } time ++; }; }; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); android.util.Log.d("RSSIService", "ALog 0925 > onCreate()-----------------------------------------"); wifiMgr = (WifiManager)getSystemService(WIFI_SERVICE); h.sendEmptyMessageDelayed(0, 2000); IntentFilter filter = new IntentFilter(); filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub alog(intent.getAction()); if(WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())){ final NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); mWifiConnected = networkInfo != null && networkInfo.isConnected(); if(mWifiConnected){ WifiInfo info = (WifiInfo) intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO); connectedSSID = info.getSSID(); } } } }; registerReceiver(receiver, filter); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } void showWifiState(){ List<ScanResult> results = wifiMgr.getScanResults(); if(results != null && results.size() > 0){ for(ScanResult sr:results){ if(mWifiConnected && sr.SSID.equals(connectedSSID)){ Intent intent = new Intent(WifiManager.RSSI_CHANGED_ACTION); intent.putExtra(WifiManager.EXTRA_NEW_RSSI, sr.level); sendBroadcast(intent); } } } } void alog(String s){ android.util.Log.d("RSSIService", "ALog 0925 > " + s + "-----------------------------------------"); } public String getWifiState(int state){ String result = "WIFI_STATE_UNKNOWN"; switch(state){ case WifiManager.WIFI_STATE_DISABLED: result = "WIFI_STATE_DISABLE"; break; case WifiManager.WIFI_STATE_DISABLING: result = "WIFI_STATE_DISABLING"; break; case WifiManager.WIFI_STATE_ENABLED: result = "WIFI_STATE_ENABLED"; break; case WifiManager.WIFI_STATE_ENABLING: result = "WIFI_STATE_ENABLING"; break; } return result; } }
主要处理思路:
获取到热点信号后,通过广播发给状态栏。