无线连接,断开连接监听器 [英] Wifi Connect-Disconnect Listener

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

问题描述

哪些监听器我的类必须实现序自动检查code如果WiFi连接/断开?

我可以手动检查无线网络连接/断开,但每次我需要从Android设置连接/断开WIFI,然后运行我的程序,结果的时间。

请帮忙。感谢名单提前。

我目前的code是简单的:

  WifiManager无线=(WifiManager)getSystemService(Context.WIFI_SERVICE);
如果(wifi.isWifiEnabled()==真)
{
    tv.setText(您已连接);
}
其他
{
    tv.setText(未连接);
}
 

解决方案

其实你检查无线网络连接是否启用的,这并不一定意味着它的连接。这只是意味着无线网络连接模式在手机上启用,并能够连接到Wi-Fi网络。

这是怎么我听着实际的Wi-Fi连接我的广播接收器:

 公共类WifiReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        ConnectivityManager赌侠=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        的NetworkInfo的NetInfo = conMan.getActiveNetworkInfo();
        如果(NetInfo的= NULL和放大器;!&安培; netInfo.getType()== ConnectivityManager.TYPE_WIFI)
            Log.d(WifiReceiver,有wifi连接);
        其他
            Log.d(WifiReceiver,别有WIFI连接);
    }
};
 

为了获得你需要以下用途,允许添加到您的Andr​​oidManifest.xml活动网络信息:

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
 

和下面的意图接收器(或您可以添加此编程...)

 <! - 接收Wi-Fi连接状态的变化 - >
<接收机器人:名称=。WifiReceiver>
    <意向滤光器>
        <作用机器人:名称=android.net.conn.CONNECTIVITY_CHANGE/>
    &所述; /意图滤光器>
< /接收器>
 


编辑:在棒棒糖,工作调度会帮助,如果你正在寻找,当用户连接到未计量的网络连接要执行的操作。请看下图:<一href="http://developer.android.com/about/versions/android-5.0.html#Power">http://developer.android.com/about/versions/android-5.0.html#Power


编辑2:另一个考虑是,我的回答不会检查你有一个连接的到互联网的。您可以连接到Wi-Fi网络,需要您签字这里是一个有用的IsOnline()检查以下内容:<一href="http://stackoverflow.com/a/27312494/1140744">http://stackoverflow.com/a/27312494/1140744

Which listener does my class have to implement inorder to automatically check code if the wifi connects/disconnects ?

I'm able to manually check for wifi connection/disconnection but each time i need to connect/disconnect WIFI from android settings and then run my program for the result.

Please help. Thanx in advance.

My current code is as simple as :

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()==true)
{
    tv.setText("You are connected");
}
else
{
    tv.setText("You are NOT connected");
}

解决方案

Actually you're checking for whether Wi-Fi is enabled, that doesn't necessarily mean that it's connected. It just means that Wi-Fi mode on the phone is enabled and able to connect to Wi-Fi networks.

This is how I'm listening for actual Wi-Fi connections in my Broadcast Receiver:

public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {     
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) 
            Log.d("WifiReceiver", "Have Wifi Connection");
        else
            Log.d("WifiReceiver", "Don't have Wifi Connection");    
    }   
};

In order to access the active network info you need to add the following uses-permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And the following intent receiver (or you could add this programmatically...)

<!-- Receive Wi-Fi connection state changes -->
<receiver android:name=".WifiReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>


EDIT: In Lollipop, Job Scheduling may help if you're looking to perform an action when the user connects to an unmetered network connection. Take a look: http://developer.android.com/about/versions/android-5.0.html#Power


EDIT 2: Another consideration is that my answer doesn't check that you have a connection to the internet. You could be connected to a Wi-Fi network which requires you to sign in. Here's a useful "IsOnline()" check: http://stackoverflow.com/a/27312494/1140744

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

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