Created
June 14, 2016 07:20
-
-
Save brucetoo/9e6113be4903805ccbf84813f70ab898 to your computer and use it in GitHub Desktop.
网络变化监听过滤掉多次回调的问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.netease.cc.dhsdkcore.utils; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.support.v4.content.LocalBroadcastManager; | |
import android.widget.Toast; | |
import com.netease.cc.dhsdkcore.config.CCRecordConfig; | |
import com.netease.cc.dhsdkcore.service.UploadService; | |
/** | |
* Created by Bruce too | |
* On 2016/4/5 | |
* At 16:45 | |
* 网络变化监听receiver | |
*/ | |
public class NetworkReceiver extends BroadcastReceiver { | |
NetworkInfo.State wifiState = null; | |
NetworkInfo.State mobileState = null; | |
public static final String ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; | |
private static int lastType = -1;//make sure we only get one notification when 2-3 comes | |
//http://stackoverflow.com/questions/8412714/broadcastreceiver-receives-multiple-identical-messages-for-one-event | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if (ACTION.equals(intent.getAction())) { | |
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); | |
mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); | |
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo(); | |
int currentType = activeNetworkInfo.getType(); | |
Intent netIntent = new Intent(CCRecordConfig.INTENT_NETWORK_STATE_CHANGE); | |
if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState && NetworkInfo.State.CONNECTED == mobileState) { | |
if(UploadService.service == null && lastType != currentType) { | |
LogUtils.info("NetworkReceiver","移动网络开启"); | |
UIHelper.makeToast(context, "切换为移动网络", Toast.LENGTH_SHORT); | |
} | |
netIntent.putExtra("networkstate", 1);// 1 表示有移动网情况 | |
} else if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED == wifiState && NetworkInfo.State.CONNECTED != mobileState) { | |
if(lastType != currentType) { | |
LogUtils.info("NetworkReceiver", "wifi网络开启"); | |
UIHelper.makeToast(context, "切换为WIFI网络", Toast.LENGTH_SHORT); | |
} | |
netIntent.putExtra("networkstate", 2);// 2 表示有wifi网情况 | |
} else if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState && NetworkInfo.State.CONNECTED != mobileState) { | |
netIntent.putExtra("networkstate", -1);// -1 表示无网情况 | |
} | |
if(lastType != currentType) { | |
LocalBroadcastManager.getInstance(context).sendBroadcast(netIntent); | |
} | |
lastType = currentType; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment