概述
1、网络状态判断
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* 网络工具
* @author fishpan
*/
public class NetWorkUtils {
public Context context;
public NetWorkUtils(Context context) {
super();
this.context = context;
}
/**
* 获取是否可用
* @param context
* @return
*/
public boolean available(){
boolean flag = false;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()){
flag = true;
}
return flag;
}
/**
* 获取当前网络类型名称
* @return mobile 手机网络,wifi wifi网络
*/
public String netowrkTypeName(){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null){
return networkInfo.getTypeName();
}
return null;
}
/**
* 手机网络是否可用
* @return
*/
public boolean mobileAvailable(){
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (networkinfo != null) {
return networkinfo.isAvailable();
}
return false;
}
/**
* wifi是否可用
* @return
*/
public boolean wifiAvailable(){
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkinfo != null) {
return networkinfo.isAvailable();
}
return false;
}
}
2、网络状态监听
a、自定义broadcastreceive
public class NetworkStateBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase(WifiManager.WIFI_STATE_CHANGED_ACTION)){
wifiStateChange(context, intent);
}else{
networkChange(context);
}
}
public void wifiStateChange(Context context, Intent intent){
int wifi_state = intent.getIntExtra("wifi_state", 0);
int level = Math.abs(((WifiManager)context.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getRssi());
Log.e("network", "wifi状态:" + wifi_state + "; wifi强度:" + level);
}
public void networkChange(Context context){
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if(networkInfo != null){
int type = networkInfo.getType();
switch (type) {
case ConnectivityManager.TYPE_MOBILE:
Log.e("network", "手机网络");
break;
case ConnectivityManager.TYPE_WIFI:
Log.e("network", "wifi网络");
break;
}
}else{
Log.e("network", "没有网络");
}
}
}
b、注册广播
NetworkStateBroadcastReceiver broadcastReceiver = new NetworkStateBroadcastReceiver();
IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, filter);
c、权限设置
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
最后
以上就是眯眯眼河马为你收集整理的Android网络状态监听,wifi强度监听的全部内容,希望文章能够帮你解决Android网络状态监听,wifi强度监听所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复