我已将ConnectivityManager.CONNECTIVITY_ACTION
操作添加到我的意图过滤器中。当Wi-Fi状态或移动数据状态发生变化时,会触发BroadcastReceiver的onReceive()
。我需要确定这种变化是由于wifi还是移动数据造成的。
或者,如果关闭移动数据,是否有任何意图过滤器?
答案 0 :(得分:0)
简介:
你在onReceive()函数中:
@Override
public void onReceive(final Context context,final Intent intent) {
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
// Use netInfo here :
// netInfo.getType()
// netInfo.getTypeName()
// netInfo.isConnected()
// ...
}
您现在可以知道更改是来自Wifi还是移动数据,以及它是否已连接
更多信息:http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html