Example usage for android.net ConnectivityManager CONNECTIVITY_ACTION

List of usage examples for android.net ConnectivityManager CONNECTIVITY_ACTION

Introduction

In this page you can find the example usage for android.net ConnectivityManager CONNECTIVITY_ACTION.

Prototype

String CONNECTIVITY_ACTION

To view the source code for android.net ConnectivityManager CONNECTIVITY_ACTION.

Click Source Link

Document

A change in network connectivity has occurred.

Usage

From source file:com.google.android.apps.muzei.NetworkChangeReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (intent == null || !ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        return;/* w w  w  .  j av  a2 s  . c o m*/
    }

    boolean hasConnectivity = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
    if (hasConnectivity) {
        EventBus.getDefault().post(new GainedNetworkConnectivityEvent());

        // Check with components that may not currently be alive but interested in
        // network connectivity changes.
        Intent retryIntent = ArtworkCache.maybeRetryDownloadDueToGainedConnectivity(context);
        if (retryIntent != null) {
            startWakefulService(context, retryIntent);
        }

        // TODO: wakeful broadcast?
        SourceManager sm = SourceManager.getInstance(context);
        sm.maybeDispatchNetworkAvailable();
    }
}

From source file:com.vadimfrolov.duorem.FragmentNet.java

@Override
public void onResume() {
    super.onResume();

    // Listening for network events
    IntentFilter filter = new IntentFilter();
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    getActivity().registerReceiver(mNetworkReceiver, filter);

    // check connection right away
    NetworkInfo ni = mConnMgr.getActiveNetworkInfo();
    mIsConnected = ni != null && ni.isConnectedOrConnecting();
}

From source file:org.microg.gms.checkin.TriggerReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    boolean force = "android.provider.Telephony.SECRET_CODE".equals(intent.getAction());
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREF_ENABLE_CHECKIN, false)
            || force) {//www  . j  ava 2s .  c  o m
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())
                && LastCheckinInfo.read(context).lastCheckin > System.currentTimeMillis()
                        - REGULAR_CHECKIN_INTERVAL) {
            return;
        }

        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected() || force) {
            Intent subIntent = new Intent(context, CheckinService.class);
            subIntent.putExtra(CheckinService.EXTRA_FORCE_CHECKIN, force);
            startWakefulService(context, subIntent);
        }
    } else {
        Log.d(TAG, "Ignoring " + intent + ": checkin is disabled");
    }
}

From source file:org.mariotaku.twidere.receiver.ConnectivityStateReceiver.java

@Override
public void onReceive(final Context context, final Intent intent) {
    if (BuildConfig.DEBUG) {
        Log.d(RECEIVER_LOGTAG, String.format("Received Broadcast %s", intent));
    }/*from  w ww.j  av a2  s.  c  o m*/
    if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
        return;
    final TwidereApplication application = TwidereApplication.Companion.getInstance(context);
    //        application.reloadConnectivitySettings();
    Utils.startRefreshServiceIfNeeded(application);
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    if (prefs.getBoolean(KEY_USAGE_STATISTICS, false)
            && prefs.getBoolean(KEY_SETTINGS_WIZARD_COMPLETED, false)) {
        // BEGIN HotMobi
        final NetworkEvent event = NetworkEvent.create(context);
        HotMobiLogger.getInstance(context).log(event);
        // END HotMobi
    }

    final Context appContext = context.getApplicationContext();
    final ConnectivityManager cm = (ConnectivityManager) appContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final boolean isNetworkMetered = ConnectivityManagerCompat.isActiveNetworkMetered(cm);
    final boolean isCharging = Utils.isCharging(appContext);
    if (!isNetworkMetered && isCharging) {
        final long currentTime = System.currentTimeMillis();
        final long lastSuccessfulTime = HotMobiLogger.getLastUploadTime(appContext);
        if ((currentTime - lastSuccessfulTime) > HotMobiLogger.UPLOAD_INTERVAL_MILLIS) {
            appContext.startService(new Intent(appContext, UploadLogsService.class));
        }
    }

}

From source file:com.tomeokin.archsample.data.remote.NetworkReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences preferences = ArchApplication.getApp()
            .getSharedPreferences(CollectManager.PREF_COLLECTION, Context.MODE_PRIVATE);
    final boolean haveCollection = preferences.getBoolean(CollectManager.HAVE_NEW_COLLECTIONS, false);
    final int count = preferences.getInt(CollectManager.NEW_COLLECTION_COUNT, 0);
    if (!haveCollection || count <= 0) {
        return;//from w w  w. ja  v  a2s.c om
    }

    Logger.t("NetworkReceiver").i("connection state change");
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifi.isConnected()) {
            Logger.t("NetworkReceiver").i("wifi is connected");
            //List<Collection> collections = CollectManager.getInstance()
            //    .getCollectionsFromPref();

            PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class),
                    0);
            Notification notification = new NotificationCompat.Builder(context)
                    .setTicker("have new collections").setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(count + " new collections")
                    //.setContentText("have new collections")
                    .setContentIntent(pi).setAutoCancel(true).build();
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(ACTION_SHOW_NOTIFICATION_REQUEST_CODE, notification);
            preferences.edit().putBoolean(CollectManager.HAVE_NEW_COLLECTIONS, false).apply();
        }
    }
}

From source file:org.videolan.vlc.gui.browser.NetworkBrowserFragment.java

public void onStart() {
    super.onStart();

    //Handle network connection state
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    getActivity().registerReceiver(networkReceiver, filter);
}

From source file:org.wso2.iot.agent.services.AgentStartupReceiver.java

@Override
public void onReceive(final Context context, Intent intent) {
    String action = intent.getAction();
    if (Constants.DEBUG_MODE_ENABLED) {
        Log.d(TAG, "Action received: " + action);
    }/*from   ww  w . j  av  a 2s.co m*/
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action) && !isNetworkConnected(context, intent)) {
        return;
    }
    if (!Preference.getBoolean(context, Constants.PreferenceFlag.DEVICE_ACTIVE)) {
        Log.e(TAG, "Device is not active");
        return;
    }
    setRecurringAlarm(context.getApplicationContext());
    if (!EventRegistry.eventListeningStarted) {
        EventRegistry registerEvent = new EventRegistry(context.getApplicationContext());
        registerEvent.register();
    }
    if (!CommonUtils.isServiceRunning(context, LocationService.class)) {
        Intent serviceIntent = new Intent(context, LocationService.class);
        context.startService(serviceIntent);
    }
    if (Intent.ACTION_BOOT_COMPLETED.equals(action)
            || Constants.AGENT_UPDATED_BROADCAST_ACTION.equals(action)) {
        if (Constants.OWNERSHIP_COSU.equals(Constants.DEFAULT_OWNERSHIP)) {
            Preference.putBoolean(context.getApplicationContext(), Constants.AGENT_FRESH_START, true);
            Intent i = new Intent(context, SplashActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

From source file:org.microg.gms.gcm.TriggerReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    boolean force = "android.provider.Telephony.SECRET_CODE".equals(intent.getAction())
            || FORCE_TRY_RECONNECT.equals(intent.getAction());
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (!GcmPrefs.get(context).isEnabled() && !force) {
        Log.d(TAG, "Ignoring " + intent + ": gcm is disabled");
        return;/*from  ww w  . j a  v a  2  s.c  om*/
    }

    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        McsService.resetCurrentDelay();
    }

    if (LastCheckinInfo.read(context).androidId == 0) {
        Log.d(TAG, "Ignoring " + intent + ": need to checkin first.");
        return;
    }

    force |= "android.intent.action.BOOT_COMPLETED".equals(intent.getAction());

    NetworkInfo networkInfo = cm.getActiveNetworkInfo();

    if (!force) {
        if (networkInfo == null || !networkInfo.isConnected()) {
            Log.d(TAG, "Ignoring " + intent + ": network is offline, scheduling new attempt.");
            McsService.scheduleReconnect(context);
            return;
        } else if (!GcmPrefs.get(context).isEnabledFor(networkInfo)) {
            Log.d(TAG, "Ignoring " + intent + ": gcm is disabled for " + networkInfo.getTypeName());
            return;
        }
    }

    if (!McsService.isConnected() || force) {
        Log.d(TAG,
                "Not connected to GCM but should be, asking the service to start up. Triggered by: " + intent);
        startWakefulService(context,
                new Intent(ACTION_CONNECT, null, context, McsService.class).putExtra(EXTRA_REASON, intent));
    } else {
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            Log.d(TAG, "Ignoring " + intent + ": service is running. schedule reconnect instead.");
            McsService.scheduleReconnect(context);
        } else {
            Log.d(TAG, "Ignoring " + intent + ": service is running. heartbeat instead.");
            startWakefulService(context, new Intent(ACTION_HEARTBEAT, null, context, McsService.class)
                    .putExtra(EXTRA_REASON, intent));
        }
    }
}

From source file:com.seadee.library.receiver.NetworkStateReceiver.java

public void regNetworkReciever(Context context, OnNetworkChangeListener l) {
    this.context = context;
    this.l = l;//w w  w .  jav a 2s .c o m

    instance = this;
    IntentFilter mFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    context.registerReceiver(this, mFilter);
}

From source file:com.mygeopay.wallet.ui.ExchangeRateLoader.java

@Override
protected void onStartLoading() {
    super.onStartLoading();

    config.registerOnSharedPreferenceChangeListener(this);

    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    intentFilter.addAction(Intent.ACTION_TIME_TICK);
    context.registerReceiver(broadcastReceiver, intentFilter);

    forceLoad();//from   w  w  w  .j  a v a2s .co m
}