Android(Java):切換3g提醒

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

發佈了51 篇原創文章 · 獲贊 2 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章