ACTION_PICK_WIFI_NETWORK与avaliable网络对话框上 [英] ACTION_PICK_WIFI_NETWORK on a dialog with avaliable networks

查看:976
本文介绍了ACTION_PICK_WIFI_NETWORK与avaliable网络对话框上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个对话框这说明像 ACTION_PICK_WIFI_NETWORK 但不是开放的 Android设置/无线打开一个对话框,如果有可能的用户可以连接从任何网络avaliable 对话。我所目前是开放对话框列表可用的Wi-Fi网络的Andr​​oid系统,但这个列表是不一样的 Android设置/无线这就是为什么我问是否有可能打开该 ACTION_PICK_WIFI_NETWORK 的对话和工作与它。如果这是不可能的我怎么能连接到网络点击一个项目从我的对话框带WiFi avaliable?

我到目前为止已经试过是

我有一个的BroadcastReceiver()

  wifiReceiver =新的BroadcastReceiver(){
@覆盖
  公共无效的onReceive(上下文C,意图意图){
       如果(mWifiManager!= NULL){
          名单< ScanResult>结果= mWifiManager.getScanResults();
          showWifiListDialog(结果);
       }
  }
};
 

A RegisterReceiver()

  registerReceiver(wifiReceiver,新的IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
 

这是一个执行方法 WifiScan

 私人无效startWifiScans(){
 mWifiManager =(WifiManager)getSystemService(Context.WIFI_SERVICE);
 mWifiManager.startScan();
}
 

这是一个简单的对话框这说明 SCAN_RESULTS_AVAILABLE_ACTION

 私人无效showWifiListDialog(名单< ScanResult>的结果){
    AlertDialog.Builder builderSingle =新AlertDialog.Builder(
            本);
    最后ArrayAdapter<字符串> arrayAdapter =新的ArrayAdapter<字符串>(
            本,
            android.R.layout.select_dialog_item);

    对于(ScanResult R:结果){
        如果(R == NULL || r.SSID == NULL)继续;
        如果(equalsIgnoreCase(r.SSID.trim()))继续;
        字符串名称= r.SSID.replace(\,);
        arrayAdapter.add(名称);
    }
    builderSingle.setNegativeButton(的getString(R.string.cancel)
            新DialogInterface.OnClickListener(){
                @覆盖
                公共无效的onClick(DialogInterface对话,诠释它){
                    dialog.dismiss();
                }
            });
    builderSingle.setAdapter(arrayAdapter,
            新DialogInterface.OnClickListener(){
                @覆盖
                公共无效的onClick(DialogInterface对话,诠释它){
                    字符串则strName = arrayAdapter.getItem(所);
                    Toast.makeText(getApplicationContext(),入选+则strName,Toast.LENGTH_SHORT).show();
                }
            });
    AlertDialog对话框= builderSingle.create();
    dialog.show();
}
 

例有影像

和<一href="http://forums.androidcentral.com/attachments/motorola-droid-maxx/165590d1425418311t-can-t-get-rid-annoying-wifi-dialog-screenshot_2015-02-19-13-10-31.png"相对=nofollow>这个就是例子还是什么我要找的。

编辑(这是我看到我的对话框的时刻,但我不喜欢它尚未....)

我想,以示与图标网​​络信号强度Android这样做的例如。我想,我需要一个ListAdapter左右?然后添加网络名称,连接强度,图标,等...难道我错了吗?

几乎同样的问题,正确的 ..

我想打开它throught一个通知,显然它并不重要,如果我在的应用程序或其他APP ......我只是想打开它作为一个对话框,让用户看到该用户在看。

现在我所得到的是这样的:

我使用了一个主题,但它不会做我想做的。

 &LT;样式名称=dialogtest父=AppTheme&GT;
    &LT;项目名称=机器人:窗框&GT; @android:彩色/透明&LT; /项目&GT;
    &LT;项目名称=机器人:windowIsFloating&GT;真&LT; /项目&GT;
    &LT;项目名称=机器人:windowIsTranslucent&GT;真&LT; /项目&GT;
    &LT;项目名称=机器人:windowNoTitle&GT;真&LT; /项目&GT;
    &LT;项目名称=机器人:背景&GT; @android:彩色/透明&LT; /项目&GT;
    &LT;项目名称=机器人:windowBackground&GT; @android:彩色/透明&LT; /项目&GT;
&LT; /风格&GT;
 

这是风格,我把它这样做:

 保护无效的onCreate(包savedInstanceState){
    setTheme(R.style.dialogtest);
    super.onCreate(savedInstanceState);
 

解决方案

从你需要一个定义适配器用户界面的角度来看:

 私有类WifiAdapter扩展ArrayAdapter&LT; ScanResult&GT; {

    公共WifiAdapter(上下文的背景下,INT资源,列表和LT; ScanResult&GT;对象){
        超(背景下,资源对象);
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        如果(convertView == NULL){
            。convertView = getLayoutInflater()膨胀(R.layout.wifi_item,父母,假);
        }
        ScanResult结果=的getItem(位置);
        ((TextView中)convertView.findViewById(R.id.wifi_name))的setText(formatSSDI(结果))。
        ((ImageView的)convertView.findViewById(R.id.wifi_img))setImageLevel(getNormalizedLevel(结果));
        返回convertView;
    }

    私人诠释getNormalizedLevel(ScanResult R){
        INT水平= WifiManager.calculateSignalLevel(r.level,
                5);
        Log.e(的getClass()getSimpleName(),电平+级);
        回报水平;
    }

    私人字符串formatSSDI(ScanResult R){
        如果(R == NULL || r.SSID == NULL ||.equalsIgnoreCase(r.SSID.trim())){
            返回无数据;
        }
        返回r.SSID.replace(\,);
    }
 

我稍微改变你的 showWifiListDialog

 私人无效showWifiListDialog(名单&LT; ScanResult&GT;的结果){
    Collections.sort(结果,新的比较&LT; ScanResult&GT;(){
        @覆盖
        公众诠释比较(ScanResult LHS,ScanResult RHS){
            返回rhs.level&GT; lhs.level? 1:rhs.level&LT; lhs.level? -1:0;
        }
    });
    AlertDialog.Builder builderSingle =新AlertDialog.Builder(
            本);
    最后WifiAdapter arrayAdapter =新WifiAdapter(
            本,
            android.R.layout.select_dialog_item,结果);

    builderSingle.setNegativeButton(的getString(android.R.string.cancel)
            新DialogInterface.OnClickListener(){
                @覆盖
                公共无效的onClick(DialogInterface对话,诠释它){
                    dialog.dismiss();
                }
            });
    builderSingle.setAdapter(arrayAdapter,
            新DialogInterface.OnClickListener(){
                @覆盖
                公共无效的onClick(DialogInterface对话,诠释它){
                    字符串则strName = arrayAdapter.getItem(这).SSID;
                    Toast.makeText(getApplicationContext(),入选+则strName,Toast.LENGTH_SHORT).show();
                }
            });
    AlertDialog对话框= builderSingle.create();
    dialog.show();
}
 

WiFi的项目是

 &LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向&GT;

    &LT;的TextView
        机器人:TEXTSIZE =16SP
        机器人:填充=5DP
        机器人:layout_gravity =center_vertical
        机器人:ID =@ + ID / wifi_name
        机器人:layout_width =0dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1/&GT;

    &LT; ImageView的
        机器人:ID =@ + ID / wifi_img
        机器人:layout_gravity =center_vertical
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@可绘制/ wifi_level/&GT;
&LT; / LinearLayout中&GT;
 

和被拉伸wifi_level是

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;电平表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
    &LT;项目
        机器人:可绘制=@可绘制/ ic_signal_wifi_0_bar_black_24dp
        机器人:maxLevel =0/&GT;
    &LT;项目
        机器人:可绘制=@可绘制/ ic_signal_wifi_1_bar_black_24dp
        机器人:maxLevel =1/&GT;
    &LT;项目
        机器人:可绘制=@可绘制/ ic_signal_wifi_2_bar_black_24dp
        机器人:maxLevel =2/&GT;
    &LT;项目
        机器人:可绘制=@可绘制/ ic_signal_wifi_3_bar_black_24dp
        机器人:maxLevel =3/&GT;

    &LT;项目
        机器人:可绘制=@可绘制/ ic_signal_wifi_4_bar_black_24dp
        机器人:maxLevel =4/&GT;
&LT; /电平列表&gt;
 

我从这里五png格式>

有关连接,得到的回答是的是的,它应该是可能的。因此该文件至少。你可以实例 WifiConfiguration 的对象,并与信息喂它网络要连接到( SSID 密码)。这不是一个简单的事情。如果你必须要考虑到不同类型的密钥加密,( WPA WEP 免费WiFi )。 Onceyou填补了对象,你必须调用

  mWifiManager.disconect();
INT渣油= mWifiManager.addNetwork(配置);
mWifiManager.enableNetwork(渣油,真正的);
 

编辑:

如果你想显示的WiFi信号强度图标有和没有锁,你可以使用自定义属性

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
    &LT;申报,设置样式名称=无线网络连接&GT;
        &LT; attr指示NAME =state_locked格式=布尔/&GT;
    &LT; /申报,设置样式&GT;
&LT; /资源&GT;
 

和在ImageView的子类更新其状态:

 公共类WifiImageView扩展ImageView的{

私有静态最终诠释[] STATE_LOCKED = {R.attr.state_locked};
私人布尔mWifiLocked;

公共WifiImageView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
}

@覆盖
公众诠释[] onCreateDrawableState(INT extraSpace){
    最终诠释[] drawableState = super.onCreateDrawableState(extraSpace + 1);
    如果(mWifiLocked){
        mergeDrawableStates(drawableState,STATE_LOCKED);
    }
    返回drawableState;
}

公共无效setStateLocked(布尔锁定){
    mWifiLocked =锁定;
    refreshDrawableState();
}
 

}

现在假设机器人:SRC你的 WifeImageView 是一种选择

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:定制=htt​​p://schemas.android.com/apk/res-auto&GT;
    &LT;项目定制:state_locked =假机器人:可绘制=@可绘制/ wifi_level/&GT;
    &LT;项目定制:state_locked =真正的机器人:可绘制=@可绘制/ wifi_level_lock/&GT;
&LT; /选择器&GT;
 

在你的适配器,你可以很容易地在两个级列表之间进行切换,加入以下两行$​​ C $ C

 布尔protectedWifi = result.capabilities.contains(WEP)|| result.capabilities.contains(WPA);
 ((WifiImageView)convertView.findViewById(R.id.wifi_img))setStateLocked(protectedWifi)。
 

protectedWifi 评估如此,如果 result.capabilities 包含 WEP WPA setStateLocked(protectedWifi); 将两者之间的 LEVEL-切换清单相应s到它的价值。当然,在 wifi_item.xml ,你从 ImageView的两个转变,以自定义 WifiImageView

I'm trying to create a Dialog which shows something like ACTION_PICK_WIFI_NETWORK but instead of open Android Settings / WiFi open it on a Dialog and if it's possible user could connect to any network avaliable from that Dialog. What I've at the moment is open a Dialog with a List of available Wi-Fi networks in Android, but this List isn't the same as Android Settings / WiFi thats why I'm asking if it's possible to open this ACTION_PICK_WIFI_NETWORK on a dialog and work with it. If it's not possible how could I connect to network clicking on a Item from my Dialog with WiFi avaliable?

What I've tried so far is

I've got 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 does the WifiScan

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

And this is the simple Dialog which shows the 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();
}

Example with images

This and this are examples or what I'm looking for.

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

I'd like to show the networks with the icon with signal strength like android does example. I think I'll need a ListAdapter or so? And then add the Network name, Strength connection, icon, etc... Am I wrong?

Almost the same question right there..

I want to open it throught a Notification and obviously it doesn't matter if I'm on that APP or other APP... I just want to open it as a dialog and let to the user see what the user was watching.

Now what I get is this :

I'm using a Theme but it doesn't 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 the style and I call it doing this :

 protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.dialogtest);
    super.onCreate(savedInstanceState);

解决方案

From the ui perspective 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 slightly 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();
}

the Wifi Item is

<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 the drawable 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 the five png from here

For the Connection, the answer is yes it should be possible. Accordingly to the documentation at least. You can instantiate an object of WifiConfiguration, and feed it with info of the network you want to connect to (SSID and password). It is not a straightforward thing to do. If you have to take in consideration the different kind of keys encryption, (WPA, WEP, free wifi). Onceyou filled up the object, you have to call

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

Edit:

If you want to show the wifi-signal-strength icon with and without padlock, you could use custom 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 assuming that the 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 the two level-list, 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 is evaluated true if result.capabilities contains WEP or WPA, and setStateLocked(protectedWifi); will switch between the two level-lists accordingly to its value. Of course, in the wifi_item.xml, you have two change from ImageView, to the custom WifiImageView.

这篇关于ACTION_PICK_WIFI_NETWORK与avaliable网络对话框上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆