等待 Android 上的 wifi 连接

发布于 2024-12-23 14:20:52 字数 179 浏览 0 评论 0原文

我有一个小程序尝试连接到 wifi 网络。 如果这是第一次连接到某些网络,它会启用设备上的 wifi。它会加载设备 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2024-12-30 14:20:52

我一个月前已经找到了您问题的解决方案,只需使用 Thread put 方法 isConnected() 即可。
在本例中,我使用 WifiExplorerActivity 来显示所有 wifi 网络并允许用户连接到它。

        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();

这是检查 wifi 是否已连接的功能:

  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;
}

最后,确保您的 Androidmanifest.xml 如下所示:

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

另外,您可以使用 ProgressDialog 来等待连接。请参阅http://developer.android.com/guide/topics/ui/dialogs。 html

I have 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;
}

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>

Also, you can use ProgressDialog to wait connect. See http://developer.android.com/guide/topics/ui/dialogs.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文