private void updateConnected() {
    ConnectivityManager cm = null;
    try {
      cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    } catch (ClassCastException cce) {
      return;
    }
    if (cm == null) return;

    NetworkInfo ni = cm.getActiveNetworkInfo();

    if (ni != null && ni.isConnectedOrConnecting()) {
      // Connected
      lastTimeConnected = new Date();
      if (disconnected.getVisibility() != GONE) {
        disconnected.setVisibility(GONE);
      }
    } else {
      // Disconnected
      if (lastTimeConnected.before(
          new Date(new Date().getTime() - DISCONNECTED_WARNING_ICON_THRESHOLD))) {
        if (disconnected.getVisibility() != VISIBLE) {
          disconnected.setVisibility(VISIBLE);
        }
      }
    }
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo activeWifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo activeHighNetInfo =
        connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI);

    boolean isMobileConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();

    boolean isHighSpeedConnected =
        activeHighNetInfo != null && activeHighNetInfo.isConnectedOrConnecting();

    boolean isWifiConnected = activeWifiInfo != null && activeWifiInfo.isConnectedOrConnecting();
    if (isWifiConnected || isMobileConnected || isHighSpeedConnected) {
      if (ApplicationLoader.getPreferences().isLoggedIn()) {
        if (!ApplicationLoader.getPreferences().isPullAlarmService()) {
          SessionManagement sm = new SessionManagement(context);
          sm.getLastIdFromPreferences();
          ApplicationLoader.setAlarm();
        }
        if (TextUtils.isEmpty(ApplicationLoader.getPreferences().getInstallationDate())) {
          ApplicationLoader.getPreferences().setInstallationDate(Utilities.getTodayDate());
        }
        context.startService(new Intent(context, PullAlarmService.class));
      }
    }
  }
Esempio n. 3
0
 private boolean isOnline(Intent intent) {
   ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getActiveNetworkInfo();
   if (netInfo != null && netInfo.isConnectedOrConnecting()) return true; // continue
   post(intent, getString(R.string.error_no_connection));
   return false;
 }
  /**
   * 是否有网络连接
   *
   * @return
   */
  public static boolean isNetworkConnected() {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();

    return info != null && info.isConnectedOrConnecting();
  }
Esempio n. 5
0
 public static boolean hasNetWorkConnectivity(Context c) {
   ConnectivityManager connectivityManager =
       (ConnectivityManager) c.getSystemService(c.CONNECTIVITY_SERVICE);
   NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
   boolean hasConnection = networkInfo != null && networkInfo.isConnectedOrConnecting();
   return hasConnection;
 }
  public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting() && netInfo.isAvailable()) return true;
    else return false;
  }
  public void onRun() {
    ConnectivityManager cm =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    list = dbHandler.getDatabyTypeFav(type);
    List<Data> EmptyList = new ArrayList<Data>();
    EmptyList.add(
        new Data(
            "No favourite added yet",
            "",
            "",
            "",
            "",
            "",
            "<Description><Desc>Tap on star icon in any event to add it as a favourite...</Desc></Description>",
            "empty"));
    if (list != null) mainlist.setAdapter(new ListAdapter(context, -1, list));
    else mainlist.setAdapter(new ListAdapter(context, -1, EmptyList));
    if (isConnected) {
      try {
        con.getDatabaseUpdate(dbHandler.getVersion());
      } catch (JSONException e) {
        e.printStackTrace();
      }
    } else {
      swipeRefreshLayout.setRefreshing(false);
      Toast.makeText(context, "Please Connect To The Internet", Toast.LENGTH_SHORT).show();
    }
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    ConnectivityManager manager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    mState = State.NOT_CONNECTED;

    NetworkInfo[] nInfos = manager.getAllNetworkInfo();
    for (NetworkInfo nInfo : nInfos) {
      if (nInfo.isConnectedOrConnecting()) {
        mNetworkInfo = nInfo;
        mState = State.CONNECTED;
        break;
      }
    }

    if (mHandlers != null) {
      // Notifiy any handlers.
      Iterator<Handler> it = mHandlers.keySet().iterator();
      while (it.hasNext()) {
        Handler target = it.next();
        Message message = Message.obtain(target, mHandlers.get(target));
        target.sendMessage(message);
      }
    }
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    if (prefs == null) {
      prefs = context.getSharedPreferences("serval", 0);
    }

    if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
      ConnectivityManager connManager =
          (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

      NetworkInfo info =
          (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
      int opp =
          (info.getType() == ConnectivityManager.TYPE_WIFI)
              ? ConnectivityManager.TYPE_MOBILE
              : ConnectivityManager.TYPE_WIFI;
      NetworkInfo other = connManager.getNetworkInfo(opp);
      Map<String, ?> idMap = prefs.getAll();

      /* Connected, add rules back */
      if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
        performAction(context, idMap, AppHostCtrl.SERVICE_REMOVE);
        performAction(context, idMap, AppHostCtrl.SERVICE_ADD);
      }
      /* Disconnected, remove rules */
      else if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
        performAction(context, idMap, AppHostCtrl.SERVICE_REMOVE);

        /* Make rules available, since other interface is up */
        if (other.isConnectedOrConnecting()) {
          performAction(context, idMap, AppHostCtrl.SERVICE_ADD);
        }
      }
    }
  }
Esempio n. 10
0
 // Checks to see if the network is available or not
 public static boolean isNetworkAvailable() {
   ConnectivityManager connectivityManager =
       (ConnectivityManager)
           RUDirectApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
   return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
 }
Esempio n. 11
0
  public static boolean isInternetAvailable(Context context) {
    ConnectivityManager cm =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return (activeNetwork != null && activeNetwork.isConnectedOrConnecting());
  }
Esempio n. 12
0
  /**
   * Used to check for a data connection. Will alert the user if connection is unavailable.
   *
   * @param _context Context
   * @return boolean to show if a data connection is available.
   */
  public boolean checkInternet(final Context _context) {

    // Create the ConnectivityManager and check for active network
    ConnectivityManager cm =
        (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
      // Connection is good
      return true;
    } else {
      // No connection, alert the user and return a false.
      new AlertDialog.Builder(_context)
          .setTitle("No Internet")
          .setMessage(
              "Internet connectivity is unavailable, please enable your internet connection and try again.")
          .setPositiveButton(
              android.R.string.yes,
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {}
              })
          .setIcon(android.R.drawable.ic_dialog_alert)
          .show();
      return false;
    }
  }
  @Override
  public void onReceive(final Context context, final Intent intent) {

    if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
      schuduleAlarm(context, intent);
    if (intent.getAction() != null
        && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
      Log.d("KeepAliveReceiver", "ConnectivityReceiver invoked...");
      boolean noConnectivity =
          intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
      if (!noConnectivity) {
        ConnectivityManager cm =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        // only when connected or while connecting...
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
          Log.d("KeepAliveReceiver", "We have internet, start update check and disable receiver!");
          // Start service with wakelock by using WakefulIntentService
          Intent backgroundIntent = new Intent(context, BackgroundService.class);
          WakefulIntentService.sendWakefulWork(context, backgroundIntent);
          // disable receiver after we started the service
          disableReceiver(context);
        }
      }
    }
  }
  private boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    return isConnected;
  }
 static String getNetworkType(TelephonyManager paramTelephonyManager) {
   try {
     if (Localytics.appContext
             .getPackageManager()
             .checkPermission(
                 "android.permission.ACCESS_WIFI_STATE", Localytics.appContext.getPackageName())
         == 0) {
       NetworkInfo localNetworkInfo =
           ((ConnectivityManager) Localytics.appContext.getSystemService("connectivity"))
               .getNetworkInfo(1);
       if (localNetworkInfo != null) {
         boolean bool = localNetworkInfo.isConnectedOrConnecting();
         if (bool) {
           return "wifi";
         }
       }
     } else {
       Localytics.Log.w(
           "Application does not have one more more of the following permissions: ACCESS_WIFI_STATE. Determining Wi-Fi connectivity is unavailable");
     }
   } catch (SecurityException localSecurityException) {
     Localytics.Log.w(
         "Application does not have the permission ACCESS_NETWORK_STATE. Determining Wi-Fi connectivity is unavailable",
         localSecurityException);
   }
   return "android_network_type_" + paramTelephonyManager.getNetworkType();
 }
Esempio n. 16
0
 @Test
 public void performNetworkStateChangeWithDisconnectedInfoIgnores() throws Exception {
   NetworkInfo info = mockNetworkInfo();
   when(info.isConnectedOrConnecting()).thenReturn(false);
   dispatcher.performNetworkStateChange(info);
   verifyZeroInteractions(service);
 }
Esempio n. 17
0
  private boolean isConnected(Context context) {
    ConnectivityManager cm =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return (activeNetwork == null ? false : activeNetwork.isConnectedOrConnecting());
  }
Esempio n. 18
0
 @Test
 public void performNetworkStateChangeWithDisconnectedInfo() {
   NetworkInfo info = mockNetworkInfo();
   when(info.isConnectedOrConnecting()).thenReturn(false);
   dispatcher.performNetworkStateChange(info);
   verify(service, times(1)).adjustThreadCount(info);
 }
Esempio n. 19
0
 /**
  * http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
  * http://developer.android.com/training/basics/network-ops/managing.html
  *
  * @param context
  * @return
  */
 public static boolean isNetworkAvailable(final Context context, final boolean mobile) {
   boolean available = false;
   try {
     final ConnectivityManager connectivitymanager =
         (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     final NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
     if ((networkinfo != null) && networkinfo.isConnectedOrConnecting()) {
       switch (networkinfo.getType()) {
         case ConnectivityManager.TYPE_WIFI:
         case ConnectivityManager.TYPE_WIMAX:
         case ConnectivityManager.TYPE_ETHERNET:
           available = true;
           break;
         case ConnectivityManager.TYPE_MOBILE:
           if (mobile) {
             available = true;
           }
           break;
       }
     }
   } catch (Throwable x) {
     DBG.m(x);
   }
   return available;
 }
 public static boolean isNetworkAvailable() {
   ConnectivityManager cm =
       (ConnectivityManager)
           TomahawkApp.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
   return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
 }
Esempio n. 21
0
 @Override
 public void onReceive(Context mContext, Intent intent) {
   try {
     if (!isFirstRegister) {
       NetworkInfo networkInfo = null;
       ConnectivityManager cm =
           (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
       networkInfo = cm.getActiveNetworkInfo();
       alog.debug("接收网络监听广播");
       if (null != networkInfo) {
         boolean isConnect = networkInfo.isConnectedOrConnecting();
         alog.debug("netBroadcastReceiver isConnect = " + isConnect);
         if (isConnect) {
           if (!isConnteced) {
             isConnteced = true;
             getData(mGameId);
           }
         }
       } else {
         // 网络断开了
         alog.debug("当前网络断开了");
         isConnteced = false;
         // loadingView.showContentNetExceptionOrNullData(true);
         loadingView.getmHandler().sendEmptyMessage(Constant.EXCEPTION);
       }
     } else {
       isFirstRegister = false;
     }
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
 public static boolean isNetworkAvailable() {
   context = MyApplication.applicationContext;
   ConnectivityManager connectivityManager =
       (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
   return (networkInfo != null) && (networkInfo.isConnectedOrConnecting());
 }
Esempio n. 23
0
  // Check for network status
  public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
      return true;
    }
    // Instantiate an AlertDialog.Builder with its constructor for the Add item button
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    // 2. Chain together various setter methods to set the dialog characteristics
    builder
        .setMessage(R.string.no_net_info)
        .setTitle(R.string.no_net)
        .setNegativeButton(
            R.string.close_add,
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                // User cancelled the dialog
              }
            });
    // Create the AlertDialog
    AlertDialog dialog = builder.create();
    dialog.show();
    return false;
  }
Esempio n. 24
0
  private void setDownloadedFilmsData() {
    this.mGridView.setAdapter(new FilmAdapter(getActivity(), sDOWNLOADED_FILMS));
    this.mSectionLabel.setText(getString(R.string.label_section_discover));

    if (sDOWNLOADED_FILMS.isEmpty()) {

      ConnectivityManager cm =
          (ConnectivityManager)
              getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
      boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

      if (!isConnected) {
        viewStub.setLayoutResource(R.layout.no_connection_layout);
        mGridView.setEmptyView(viewStub);
      } else {
        viewStub.setLayoutResource(R.layout.list_empty_layout);
        mGridView.setEmptyView(viewStub);
        /* Starting Download Service */
        DownloadResultReceiver mReceiver = new DownloadResultReceiver(new Handler());
        mReceiver.setReceiver(this);
        Intent intent =
            new Intent(Intent.ACTION_SYNC, null, this.getActivity(), FilmDownloadService.class);

        /* Send optional extras to Download IntentService */
        intent.putExtra(FilmDownloadService.RECEIVER_KEY, mReceiver);
        this.getActivity().startService(intent);
      }
    } else {
      setSecondFragmentContent(sDOWNLOADED_FILMS.get(0));
    }
  }
 // Check if network is available
 public static boolean networkAvailable(Activity activity) {
   ConnectivityManager connectivityManager =
       (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
   if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting()) return true;
   // showNetworkUnavailableDialog(activity);
   return false;
 }
 public boolean shouldRunIfNotOnWifi(Context context) {
   NetworkInfo ni =
       ((ConnectivityManager) context.getSystemService("connectivity")).getNetworkInfo(1);
   if (ni == null || ni.isConnectedOrConnecting()) {
     return false;
   }
   return true;
 }
Esempio n. 27
0
  protected boolean checkConnectivity() {
    ConnectivityManager userConnection =
        (ConnectivityManager)
            getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo connectInfo = userConnection.getActiveNetworkInfo();

    return (connectInfo != null && connectInfo.isConnectedOrConnecting());
  }
Esempio n. 28
0
  /**
   * Auther:张宇辉 User:zhangyuhui 2015年1月5日 上午9:59:16 Project_Name:DFram Description:TODO(判断是否是wifi连接)
   * Throws Return:boolean
   */
  public static boolean isWifi(Context context) {

    ConnectivityManager connectivityManager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    return networkInfo.isConnectedOrConnecting();
  }
 public static boolean isConnectedOrConnecting(Context context) {
   ConnectivityManager connectivityManager =
       (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
   if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
     return true;
   }
   return false;
 }
Esempio n. 30
0
 /**
  * Simple network connection check.
  *
  * @param context
  */
 private void checkConnection(Context context) {
   final ConnectivityManager cm =
       (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
   if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) {
     //            Toast.makeText(context, "No network connection.", Toast.LENGTH_LONG).show();
     Log.e(TAG, "checkConnection - no connection found");
   }
 }