ACTION_PICK_WIFI_NETWORK in the dialog box with available networks

I am trying to create a Dialog that shows something like ACTION_PICK_WIFI_NETWORK , but instead of open Android Settings / WiFi open it on Dialog and if it can connect to any network accessible from this Dialog , What I currently have is open Dialog with the List available Wi-Fi networks in Android, but this List does not match Android Settings / WiFi , so I ask if it is possible to open this ACTION_PICK_WIFI_NETWORK in a dialog box and work with it. If this is not possible, how can I connect to the network by clicking on Item from my Dialog with WiFi available?

I tried so far

I have a BroadcastReceiver()

 wifiReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context c, Intent intent){ if(mWifiManager != null) { List<ScanResult> results = mWifiManager.getScanResults(); showWifiListDialog(results); } } }; 

A RegisterReceiver()

 registerReceiver(wifiReceiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 

This is the method that WifiScan performs WifiScan

 private void startWifiScans() { mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); mWifiManager.startScan(); } 

And this is a simple Dialog that shows SCAN_RESULTS_AVAILABLE_ACTION

 private void showWifiListDialog(List<ScanResult> results) { AlertDialog.Builder builderSingle = new AlertDialog.Builder( this); final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( this, android.R.layout.select_dialog_item); for (ScanResult r: results) { if(r == null || r.SSID == null) continue; if("".equalsIgnoreCase(r.SSID.trim())) continue; String name = r.SSID.replace("\"", ""); arrayAdapter.add(name); } builderSingle.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String strName = arrayAdapter.getItem(which); Toast.makeText(getApplicationContext(),"Selected "+strName, Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builderSingle.create(); dialog.show(); } 

Image example

This and this are examples or what I am looking for.

EDIT (This is how I see my Dialog at the moment, but I don't like it yet ....)

enter image description here

I would like to show networks using icon with signal strength, for example, android makes an example . I think I need a ListAdapter or so? And then add the network name, connection strength, icon, etc. Am I mistaken?

Almost the same question is right there ..

I want to open it through Notification , and obviously it doesn’t matter if I am in this APP or another APP ... I just want to open it as a dialog and let the user know what the User was looking at.

Now I get the following:

enter image description here

I use Theme , but it does not do what I want.

 <style name="dialogtest" parent="AppTheme"> <item name="android:windowFrame">@android:color/transparent</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> </style> 

This is a style, and I call it the following:

  protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.dialogtest); super.onCreate(savedInstanceState); 
+2
android
Jul 01 '15 at 13:16
source share
1 answer

In terms of ui, you need a custom adapter:

 private class WifiAdapter extends ArrayAdapter<ScanResult> { public WifiAdapter(Context context, int resource, List<ScanResult> objects) { super(context, resource, objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = getLayoutInflater().inflate(R.layout.wifi_item, parent, false); } ScanResult result = getItem(position); ((TextView) convertView.findViewById(R.id.wifi_name)).setText(formatSSDI(result)); ((ImageView) convertView.findViewById(R.id.wifi_img)).setImageLevel(getNormalizedLevel(result)); return convertView; } private int getNormalizedLevel(ScanResult r) { int level = WifiManager.calculateSignalLevel(r.level, 5); Log.e(getClass().getSimpleName(), "level " + level); return level; } private String formatSSDI(ScanResult r) { if (r == null || r.SSID == null || "".equalsIgnoreCase(r.SSID.trim())) { return "no data"; } return r.SSID.replace("\"", ""); } 

I changed your showWifiListDialog :

 private void showWifiListDialog(List<ScanResult> results) { Collections.sort(results, new Comparator<ScanResult>() { @Override public int compare(ScanResult lhs, ScanResult rhs) { return rhs.level > lhs.level ? 1 : rhs.level < lhs.level ? -1 : 0; } }); AlertDialog.Builder builderSingle = new AlertDialog.Builder( this); final WifiAdapter arrayAdapter = new WifiAdapter( this, android.R.layout.select_dialog_item, results); builderSingle.setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String strName = arrayAdapter.getItem(which).SSID; Toast.makeText(getApplicationContext(), "Selected " + strName, Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builderSingle.create(); dialog.show(); } 

Wifi Element -

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:textSize="16sp" android:padding="5dp" android:layout_gravity="center_vertical" android:id="@+id/wifi_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <ImageView android:id="@+id/wifi_img" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/wifi_level" /> </LinearLayout> 

and valid wifi_level is

 <?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ic_signal_wifi_0_bar_black_24dp" android:maxLevel="0" /> <item android:drawable="@drawable/ic_signal_wifi_1_bar_black_24dp" android:maxLevel="1" /> <item android:drawable="@drawable/ic_signal_wifi_2_bar_black_24dp" android:maxLevel="2" /> <item android:drawable="@drawable/ic_signal_wifi_3_bar_black_24dp" android:maxLevel="3" /> <item android:drawable="@drawable/ic_signal_wifi_4_bar_black_24dp" android:maxLevel="4" /> </level-list> 

I took five png from here

For the connection, the answer is yes, it should be possible . According to the documentation, at least. You can create a WifiConfiguration object and pass it the information about the network you want to connect to ( SSID and password ). This is not a simple thing. If you must consider the different types of key encryption, ( WPA , WEP , free wifi ). When you fill out the object, you must call

 mWifiManager.disconect(); int resId = mWifiManager.addNetwork(config); mWifiManager.enableNetwork(resId, true); 

Edit:

If you want to show the wii-signal-strength icon with and without a lock, you can use your own attribute

 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="wifi"> <attr name="state_locked" format="boolean" /> </declare-styleable> </resources> 

and update its state in a subclass of ImageView:

  public class WifiImageView extends ImageView { private static final int[] STATE_LOCKED = {R.attr.state_locked}; private boolean mWifiLocked; public WifiImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public int[] onCreateDrawableState(int extraSpace) { final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); if (mWifiLocked) { mergeDrawableStates(drawableState, STATE_LOCKED); } return drawableState; } public void setStateLocked(boolean locked) { mWifiLocked = locked; refreshDrawableState(); } 

}

Now suppose android: src of your WifeImageView is a selector

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto"> <item custom:state_locked="false" android:drawable="@drawable/wifi_level" /> <item custom:state_locked="true" android:drawable="@drawable/wifi_level_lock" /> </selector> 

In your adapter, you can easily switch between two level-list by adding the following two lines of code

  boolean protectedWifi = result.capabilities.contains ("WEP") || result.capabilities.contains("WPA"); ((WifiImageView) convertView.findViewById(R.id.wifi_img)).setStateLocked(protectedWifi); 

protectedWifi evaluates to true if result.capabilities contains WEP or WPA , and setStateLocked(protectedWifi); will switch between two level-list according to its value. Of course, in wifi_item.xml you have two changes from ImageView to custom WifiImageView .

+5
Jul 04 '15 at 10:41
source share



All Articles