当前位置:   article > 正文

Android(Java):切换3g提醒_android 设置3g4g java代码

android 设置3g4g java代码

<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;
 }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号