I want to implement things like the Android status bar and the filter list: the current Wi-Fi signal status icon is displayed after the link, and the signal strength of the surrounding links is displayed after the scan.
this question code mainly refers to this article:
implementation is all on one line.
in the hashmap traversal, I tried to get multiple effects:
Code:
this.adapter = new SimpleAdapter( //
MainActivity.this, //
arraylist, R.layout.wifi_scanresults_row, //
new String[]{ITEM_KEY},
new int[]{R.id.wifi_item}); //IDtextView
/*new int[]{R.id.wifi_security};
new int[]{R.id.wifi_StrongImage});*/
listView.setAdapter(this.adapter); //
...
@SuppressLint("StringFormatInvalid")
@Override
public void onClick(View v) {
arraylist.clear(); //
wifi.startScan(); //Wi-Fi
try { //
size = size - 1;
while (size >= 0) { //size0
HashMap<String, String> item = new HashMap<>();
//wifiname.setText(getString(R.string.wifi_name, "" + results.get(size).SSID));
//wifisec.setText(getString(R.string.wifi_security, "" + results.get(size).capabilities));
// wifiImage.setImageLevel(getLayoutInflater(R.layout.wifi_sel, "" + listView.get()));
item.put(ITEM_KEY, results.get(size).SSID + "\n" + results.get(size).capabilities); //Wi-Fi
arraylist.add(item); //wifi
size--; //size
adapter.notifyDataSetChanged(); //
}
} catch (Exception e) //
{ }
my xml:
main.wifi_connect, main scan results, refer to the first link, and use the hashmap traversal method to get.
layout of scan_resuals_row: scan results, I try to make it similar to the system scan Wi-Fi list, but imageview can"t show it.
reference link: Android connection specifies Wifi
but he uses RecyclerView,. I use the ListView, switch in the first link. Is it expensive to switch?
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="...dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:paddingStart="10dp"
android:paddingEnd="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:paddingStart="10dp"
android:paddingEnd="10dp"
>
<TextView
android:id="@+id/wifi_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="@string/wifi_name"
android:textSize="18sp" />
<TextView
android:id="@+id/wifi_security"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wifi_security"
android:textSize="14sp"/>
</LinearLayout>
<ImageView
android:id="@+id/wifi_StrongImage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:scaleType="fitXY"
/>
</LinearLayout>
</RelativeLayout>
effect displayed:
figure shows the effect of selecting ImageView.
< H2 > second question < / H2 > reference link to switch icons based on strength:
Android method to get the currently connected wifi signal strength
I created the xml:
selector.xml: switch signal strength icon selector, refer to the second link.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/ic_signal_wifi_4_bar_black_24dp">
</item>
<item
android:state_pressed="false"
android:drawable="@drawable/ic_perm_scan_wifi_black_24dp">
</item>
<item
android:state_window_focused="false"
android:drawable="@drawable/ic_signal_wifi_statusbar_null_black_26x24dp">
</item>
</selector>
at present, my code just clicks on the button changed code, and does not get the surrounding Wi-Fi code, perhaps because it is annotated by itself in the code, or because you can only get the surrounding Wi-Fi, but the imageview with the same layout as the first question does not show up.
in the declaration of wifi message processing, whether the different state of Wi-Fi react or not, whether it is connected or not, what does this have to do with the value of Wi-Fi (that is, signal strength)?
// wifi
private BroadcastReceiver wifiIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int wifi_state = intent.getIntExtra("wifi_state", 0);
int level = Math.abs(((WifiManager)getSystemService(WIFI_SERVICE)).getConnectionInfo().getRssi());
switch (wifi_state) {
case WifiManager.WIFI_STATE_DISABLING:
wifi_image.setImageResource(R.drawable.wifi_sel);
wifi_image.setImageLevel(level);
break;
case WifiManager.WIFI_STATE_DISABLED:
wifi_image.setImageResource(R.drawable.wifi_sel);
wifi_image.setImageLevel(level);
break;
case WifiManager.WIFI_STATE_ENABLING:
wifi_image.setImageResource(R.drawable.wifi_sel);
wifi_image.setImageLevel(level);
break;
case WifiManager.WIFI_STATE_ENABLED:
wifi_image.setImageResource(R.drawable.wifi_sel);
wifi_image.setImageLevel(level);
break;
case WifiManager.WIFI_STATE_UNKNOWN:
wifi_image.setImageResource(R.drawable.wifi_sel);
wifi_image.setImageLevel(level);
break;
}
}
};
Thank you.