android广播网络状态,Android网络切换状态广播

直接上代码:

*package com.juyoulicai.forexproduct.Service;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.widget.Toast;

import com.juyoulicai.eventbus.ForexDateEvent;

import com.juyoulicai.util.MLog;

import org.greenrobot.eventbus.EventBus;

/** * 作者: allen on 16/5/30. */

public class ConnectionChangeReceiver extends BroadcastReceiver {

private static final String TAG =ConnectionChangeReceiver.class.getSimpleName();

@Override

public void onReceive(Context context, Intent intent) {

MLog.d(TAG,"网络状态改变");

boolean success =false;

/** * 获得网络连接服务 */

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo.State state =connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

if (NetworkInfo.State.CONNECTED==state){

success =true;

}

state =connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

if (NetworkInfo.State.CONNECTED==state){

success =true;

}

if (!success){

Toast.makeText(context,"网络连接失败",Toast.LENGTH_LONG).show();

EventBus.getDefault().post(new ForexDateEvent(ForexDateEvent.NetworkInfoState,false));

} else {

EventBus.getDefault().post(new ForexDateEvent(ForexDateEvent.NetworkInfoState,true));

}

}

}

记得在Manifest文件里面进行权限声明,和广播接收器注册。

< !– Needed to check when the network connection changes –>

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

< receiver android:name="you_package_name.ConnectionChangeReceiver" android:label="NetworkConnection">

< intent-filter>

< action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>

< /intent-filter>

< /receiver>

* 使用方式一:*

在Activity的onCreate中:

//注册网络监听

IntentFilter filter = new IntentFilter();

filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

registerReceiver(mNetworkStateReceiver, filter);

在Activity中的onDestroy中:

//取消监听 unregisterReceiver(mNetworkStateReceiver);

使用方式二:

1. 应用启动时,启动Service,在Service的onCreate方法中注册网络监听:

//注册网络监听

IntentFilter filter = new IntentFilter();

filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

registerReceiver(mNetworkStateReceiver, filter);

2. 应用退出时,Service关闭,在Service的onDestroy方法中取消监听:

//取消监听

unregisterReceiver(mNetworkStateReceiver);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值