4.1.3.9 监听网络状态,3G,WIFI,

注意了:android.net.conn.CONNECTIVITY_CHANGE 是用来监听网络变化的就是ConnectivityManager.CONNECTIVITY_ACTION


监听网络的状态:
1: 第一步就是注册权限,这个权限是,ACCESS_NETWORK_STATE,ACCESS_WIFI_STATE,INTERNET
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

2: 然后创建一个BroadcastReceiver,里面有一个onReceiver();

3: 在这个onReceiver里面来判断是否是这个ConnectivityManager.CONNECTIVITY_ACTION
如果是的话,就获取当前活动的连接,但是首先要获取当前的
ConnectivityManager cm = context.getSystemService(Service.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActivtNetWork();
int type = networkInfo.getType();
int state = networkInfo.getState();
switch(types){
case ConnectivityManager.TYPE_WIFI:
if(state = networkInfo.State.CONNECTED)
break;
....

}

package tech.androidstudio.connectionbroadcastreceiverdemo;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;

/**
 * Created by Kodulf on 2016/2/29.
 */
public class ConnectionBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("NetworkReceiver", "收到广播消息" +intent );
        String action = intent.getAction();
        if(ConnectivityManager.CONNECTIVITY_ACTION.equals(action)){
            ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Service.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            if(activeNetwork!=null){
                int subtype = activeNetwork.getSubtype();
                NetworkInfo.State state = activeNetwork.getState();
                int type = activeNetwork.getType();
                switch (type){
                    case ConnectivityManager.TYPE_WIFI:
                        if(state==NetworkInfo.State.CONNECTED){
                            Log.d("Kodulf","WIFI Connected++++++++++++++++");
                        }else{
                            Log.d("Kodulf","WIFI DisConnected+++++++++++++++++++");
                        }
                        break;
                    case ConnectivityManager.TYPE_MOBILE:
                        if(state==NetworkInfo.State.CONNECTED){
                            Log.d("Kodulf","Mobile Connected+++++++++++++++++++");
                        }else{
                            Log.d("Kodulf","Mobile DisConnected+++++++++++++++++++");
                        }
                        break;
                    case ConnectivityManager.TYPE_BLUETOOTH:
                        if(state==NetworkInfo.State.CONNECTED){
                            Log.d("Kodulf","BLUETOOTH Connected+++++++++++++++++++");
                        }else{
                            Log.d("Kodulf","BLUETOOTH DisConnected+++++++++++++++++++");
                        }
                        break;
                }
            }

        }
    }
}


4:然后在清单文件中添加recevier,这个有一个优先级为1000

<receiver android:name=".ConnectionBroadcastReceiver">
    <intent-filter android:priority="1000">
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
    </intent-filter>
</receiver>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值