<receiver android:name=".broadcast.Switch3GReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

Activity activity = GlobalModel.getInstance().getCurrentActivity();
if(null == activity){
return;
}
notifySwitch3G(activity);
}

public void notifySwitch3G(final Context context) {

ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(mobNetInfo == null)
return;
if (mobNetInfo.isConnected()) {
SharedPreferences sp = context.getSharedPreferences("3gnotify", Context.MODE_PRIVATE);
boolean isCheck = sp.getBoolean(GlobalModel.getInstance().getUser().getUid() + "_3gnotify", false);
if (isCheck) {
new AlertDialog.Builder(context)
.setTitle("提示")
.setMessage("是否使用3G移动数据进行使用")
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Activity act = (Activity) context;
act.finish();
}
})
.setPositiveButton("确定",null).show();
}
}
}

/**
* 网络是否可用
* @return
*/
public static boolean isNetworkConnected(Context context){

if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}

/**
* 得到当前网络类型 3G或者WIFI
* @return
*/
public static int getNetworkType(Context context){
if(is3G(context)){
return ConnectivityManager.TYPE_MOBILE;
}else if(isWifi(context)){
return ConnectivityManager.TYPE_WIFI;
}
return 0;
}

/**
* 判断当前网络是否为wifi
*
* @param mContext
* @return
*/
public static boolean isWifi(Context mContext) {

ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}

/**
* 判断当前网络是否是3G网络
*
* @param context
* @return boolean
*/
public static boolean is3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}

更多相关文章

  1. 关于android中网络图片下载中oom解决开源框架Afinal的探究
  2. Android(五)数据存储之五网络数据交互
  3. Android Studio如何设置自己主动提示代码
  4. Android双机(网络和USB)调试及其完美ROOT
  5. Android Studio 开启调试 时 提示:could not connect to remote p
  6. 3步解决AS提示:Compilation is not supported for following modu
  7. android > 页面加载中,友情提示界面
  8. Android如何获取网络连接状态及怎样调用网络配置界面

随机推荐

  1. android 应用 安装 删除
  2. android_常用UI控件_02_EditText_01添加
  3. Android屏幕属性应用
  4. MTK Android 编译命令
  5. Android 倒影实现算法
  6. Android(安卓)Service 启动流程
  7. android 浮窗
  8. R文件没有生成或报错
  9. Android SDK无法更新终极解决方式
  10. Android 读取XML的两种方法。