博客 http://blog.csdn.net/androidbluetooth/article/details/6860146 详细的粘贴了很多判断网络的方法。

推荐一个网站,关于人工智能教程,教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点 这里 可以跳转到教程。

最近,遇到这样一个需求:

手机可以随时监听网络状态,如果网络状态发生变化要及时的更新 app 信息通知用户。

实现这个需求,有个较好的办法(个人认为,你一定有更好的办法,希望分享),分享给大家!

随时监听,需要实现一个 service 在后台监听网络状态,那麽如何接收到网络状态发生变化的信息呢?

恩,当然是 BroadcastReceiver.

网络状态发生变化的时候,系统会发出 android.net.conn.CONNECTIVITY_CHANGE .

该值详细描述如下:

public static final String CONNECTIVITY_ACTION

Since: API Level 1
A change in network connectivity has occurred. 
A connection has either been established or lost. 
The NetworkInfo for the affected network is sent as an extra; 
it should be consulted to see what kind of connectivity event occurred.
If this is a connection that was the result of failing over from a disconnected network, 
then the FAILOVER_CONNECTION boolean extra is set to true.
For a loss of connectivity, 
if the connectivity manager is attempting to connect (or has already connected) to another network, 
the NetworkInfo for the new network is also passed as an extra. 
This lets any receivers of the broadcast know that they should not necessarily tell the user that no data traffic will be possible.
Instead, the reciever should expect another broadcast soon, 
indicating either that the failover attempt succeeded (and so there is still overall data connectivity),
or that the failover attempt failed, meaning that all connectivity has been lost.
For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY is set to true if there are no connected networks at all.
Constant Value: "android.net.conn.CONNECTIVITY_CHANGE"

这是 ConnectivityManager 类的一个常量。

ok,下面是实现的 demo :


package mark.zhang;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.util.Log;

public class ListenNetStateService extends Service {
    private ConnectivityManager connectivityManager;
    private NetworkInfo info;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                Log.d("mark", "网络状态已经改变");
                connectivityManager = (ConnectivityManager)      

                                         getSystemService(Context.CONNECTIVITY_SERVICE);
                info = connectivityManager.getActiveNetworkInfo();  
                if(info != null && info.isAvailable()) {
                    String name = info.getTypeName();
                    Log.d("mark", "当前网络名称:" + name);
                } else {
                    Log.d("mark", "没有可用网络");
                }
            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mReceiver, mFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
}

 

在 manifest 文件中需要加上一条权限:

 

回头再看看关于 CONNECTIVITY_ACTION 的介绍,从 api 中,我们还可以得到一个信息:

通过 intent 可以获取一些 EXTRA,如 EXTRA_NO_CONNECTIVITY。

boolean b = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);

更多信息可以参考 ConnectivityManager.

 

更多相关文章

  1. Android(安卓)WIFI检测与设置
  2. Android调用C# .net 的WebService接口
  3. Android(安卓)仿淘宝订单状态tab(可以滑动、带红点badge)
  4. Android的Listener监听事件分析
  5. Android(安卓)电话窃听器
  6. Android(安卓)9.0 Phone对象解析
  7. Android(安卓)监听键盘弹出关闭
  8. Android如何监听蓝牙耳机的按键事件
  9. AIDL基本用法

随机推荐

  1. 同步异步多线程这三者关系,你能给面试官一
  2. 内存迟迟下不去,可能你就差一个GC.Collect
  3. 我是如何一步步的在并行编程中将lock锁次
  4. 教你配置windows上的windbg,linux上的lld
  5. async,await执行流看不懂?看完这篇以后再
  6. 不要把异常当做业务逻辑,这性能可能你无法
  7. 用了这么多年的泛型,你对它到底有多了解?
  8. 我的第26个代码
  9. 用long类型让我出了次生产事故,写代码还是
  10. 还不明白可空类型原理? 我可要挖到底了