我正在尝试在我的Android应用程序上显示图像(表示wifi信号强度的基本图像)。我已经尝试使用以下代码实现它
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.wifi_row, parent, false);
S_ID = (TextView) view.findViewById(R.id.SSID);
BSSID = (TextView) view.findViewById(R.id.BSSID);
wifi_strength = (ImageView) view.findViewById(R.id.wifi_signal);
wifi_lock = (ImageView) view.findViewById(R.id.wifi_lock);
S_ID.setText(my_array.get(position));
BSSID.setText(wifiScanList.get(position).BSSID);
int current_level = 5;
int signal_level = WifiManager.calculateSignalLevel(mainWifiObj
.getConnectionInfo().getRssi(), current_level);
int signal_difference = (signal_level * 100) / current_level;
Log.d("Signal_level", String.valueOf(signal_level));
if (signal_difference >= 100) {
wifi_strength.setImageResource(R.drawable.wifi_signal_4);
} else if (signal_difference >= 75) {
wifi_strength.setImageResource(R.drawable.wifi_signal_3);
} else if (signal_difference >= 50) {
wifi_strength.setImageResource(R.drawable.wifi_signal_2);
} else if (signal_difference >= 25) {
wifi_strength.setImageResource(R.drawable.wifi_signal_1);
}

以下是我的接收器类
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 {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
wifiScanList = mainWifiObj.getScanResults();
for (int i = 0; i < wifiScanList.size(); i++) {
my_array.add((wifiScanList.get(i)).SSID);
}
myAdapter.notifyDataSetChanged();
}
}
&#13;
但它始终显示一个完整的信号wifi图像,即使对于最弱的wifi网络。
关于如何做到这一点的任何想法?
答案 0 :(得分:2)
我自己解决了这个问题。使用hashMap存储SSID和相应的信号强度。在getView方法中,我使用SSID(Key)检索信号级别(值)。
以下代码为我工作
int wifiSignalLevel = wifi_level.get(my_array.get(position));
Log.d(my_array.get(position) + " Strength ",
String.valueOf(wifiSignalLevel));
if (wifiSignalLevel >= -70) {
wifi_strength.setImageResource(R.drawable.wifi_signal_4);
} else if (wifiSignalLevel >= -80) {
wifi_strength.setImageResource(R.drawable.wifi_signal_3);
} else if (wifiSignalLevel >= -90) {
wifi_strength.setImageResource(R.drawable.wifi_signal_2);
} else if (wifiSignalLevel >= -100) {
wifi_strength.setImageResource(R.drawable.wifi_signal_1);
}