等到无线连接在机器人 [英] wait until wifi connected on android

查看:177
本文介绍了等到无线连接在机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有试图连接到WiFi网络,一个小程序。 它使设备上的无线然后,如果是第一次,是连接到某些networkss它装上设备的WiFi,以便选择并添加密码进行连接。 直到我为了连接程序添加密码不应该结束。 如何添加东西,等到我从WiFi管理器连接得到什么呢? 我试着睡觉,但冻结的应用程序,我没有得到的wifi弹出菜单进行连接? 还有没有别的办法?

I have a small program that trying to connect to a wifi network. It enables the wifi on the device then if it's the first time that is connect to the certain networkss It loads the device wifi in order to select and add the password for connection. Until I add the password in order to connect the program should not be finished. How can I add something to wait until I get from the wifi manager that is connected? I try sleep but it freeze the application and am not getting the wifi pop up menu to connect? are there any other ways?

推荐答案

我已找到问题的解决方案在一个月前,只需要使用它线程put方法isConnected()。
在这种情况下,我用WifiExplorerActivity来显示所有的WiFi网络,并允许用户进行连接。

I has found the solution for your problem a month ago, just use Thread put method isConnected() in it.
In this case, I use WifiExplorerActivity to display all wifi network and allow user connect to it.

        Thread t = new Thread() {
        @Override
        public void run() {
            try {
                    //check if connected!
                while (!isConnected(WifiExplorerActivity.this)) {
                    //Wait to connect
                    Thread.sleep(1000);                     
                }

                Intent i = new Intent(WifiExplorerActivity.this, MainActivity.class);
                startActivity(i);

            } catch (Exception e) {
            }
        }
    };
    t.start();

这是功能来检查无线网络连接已经与否:

And this is function to check wifi has connected or not:

  public static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo = connectivityManager.getActiveNetworkInfo();
    }

    return networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED;
}

最后,请确保您的Andr​​oidManifest.xml是这样的:

Finally, make sure your Androidmanifest.xml look like this:

    <activity android:name=".WifiExplorerActivity" >        
    <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
</activity>

此外,您还可以使用ProgressDialog等待连接。请参阅<一href="http://developer.android.com/guide/topics/ui/dialogs.html">http://developer.android.com/guide/topics/ui/dialogs.html

这篇关于等到无线连接在机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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