如何知道,当用户开启/关闭移动数据或WiFi? [英] How to know when the user turn on/off mobile data or wifi?

查看:157
本文介绍了如何知道,当用户开启/关闭移动数据或WiFi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要知道当WiFi或移动数据已打开或关闭用户。其实这显示了当网络发生变化,如果设备有连接与否,做工精细。但我想,当用户手动接通/关断网络konw

右键知道我有这个在我的清单。

 <接收机器人:名字=pe.com.gps.broadcastreceivers.CheckForMobileDataBroadcastReceiver>
       &所述;意图滤光器>
           <作用机器人:名字=android.net.conn.CONNECTIVITY_CHANGE/>
       &所述; /意图滤光器>
   < /接收器>


解决方案

在onCreate方法(或当你想知道WiFi是连接或不)使用 WifiManager 。例如:

  WifiManager WM =(WifiManager)getSystemService(WIFI_SERVICE);如果(!wm.isWifiEnabled()){
   // wm.setWifiEnabled(真);
   //或者你想要什么模式为Disable
}其他{
    // wm.setWifiEnabled(假);
    //或做你想在激活模式
}

在这种情况下的 ConnectivityManager 还帮你。勾选此<一个href=\"http://stackoverflow.com/questions/12806709/android-how-to-tell-if-mobile-network-data-is-enabled-or-disabled-even-when\">conversation.

My application needs to know when the Wifi or the Mobile data has been turned on or off by the user. Actually it shows when the network changes, if the device has connection or not, works fine. but I want to konw when the user is turning on/off the network manually.

Right know I have this on my Manifest.

<receiver android:name="pe.com.gps.broadcastreceivers.CheckForMobileDataBroadcastReceiver" >
       <intent-filter>
           <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
       </intent-filter>
   </receiver>   

解决方案

In onCreate method (or whenever you want to know that wifi is connected or not) use WifiManager . For example:

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);

if (!wm.isWifiEnabled()) {
   // wm.setWifiEnabled(true);
   //Or do what you want in disable mode
} else {
    // wm.setWifiEnabled(false);
    //Or do what you want in enable mode
}

In this case ConnectivityManager also help you. Check this conversation.

这篇关于如何知道,当用户开启/关闭移动数据或WiFi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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