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.android.exchange.SyncManager.java

public void run() {
    sStop = false;/* w w  w. ja  v a2s  .  co m*/
    alwaysLog("!!! SyncManager thread running");
    // If we're really debugging, turn on all logging
    if (Eas.DEBUG) {
        Eas.USER_LOG = true;
        Eas.PARSER_LOG = true;
        Eas.FILE_LOG = true;
    }

    // If we need to wait for the debugger, do so
    if (Eas.WAIT_DEBUG) {
        Debug.waitForDebugger();
    }

    // Synchronize here to prevent a shutdown from happening while we initialize our observers
    // and receivers
    synchronized (sSyncLock) {
        if (INSTANCE != null) {
            mResolver = getContentResolver();

            // Set up our observers; we need them to know when to start/stop various syncs based
            // on the insert/delete/update of mailboxes and accounts
            // We also observe synced messages to trigger upsyncs at the appropriate time
            mAccountObserver = new AccountObserver(mHandler);
            mResolver.registerContentObserver(Account.CONTENT_URI, true, mAccountObserver);
            mMailboxObserver = new MailboxObserver(mHandler);
            mResolver.registerContentObserver(Mailbox.CONTENT_URI, false, mMailboxObserver);
            mSyncedMessageObserver = new SyncedMessageObserver(mHandler);
            mResolver.registerContentObserver(Message.SYNCED_CONTENT_URI, true, mSyncedMessageObserver);
            mMessageObserver = new MessageObserver(mHandler);
            mResolver.registerContentObserver(Message.CONTENT_URI, true, mMessageObserver);
            mSyncStatusObserver = new EasSyncStatusObserver();
            mStatusChangeListener = ContentResolver
                    .addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, mSyncStatusObserver);

            // Set up our observer for AccountManager
            mAccountsUpdatedListener = new EasAccountsUpdatedListener();
            AccountManager.get(getApplication()).addOnAccountsUpdatedListener(mAccountsUpdatedListener,
                    mHandler, true);

            // Set up receivers for connectivity and background data setting
            mConnectivityReceiver = new ConnectivityReceiver();
            registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

            mBackgroundDataSettingReceiver = new ConnectivityReceiver();
            registerReceiver(mBackgroundDataSettingReceiver,
                    new IntentFilter(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED));
            // Save away the current background data setting; we'll keep track of it with the
            // receiver we just registered
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            mBackgroundData = cm.getBackgroundDataSetting();

            // See if any settings have changed while we weren't running...
            checkPIMSyncSettings();
        }
    }

    try {
        // Loop indefinitely until we're shut down
        while (!sStop) {
            runAwake(SYNC_MANAGER_ID);
            waitForConnectivity();
            mNextWaitReason = "Heartbeat";
            long nextWait = checkMailboxes();
            try {
                synchronized (this) {
                    if (!mKicked) {
                        if (nextWait < 0) {
                            log("Negative wait? Setting to 1s");
                            nextWait = 1 * SECONDS;
                        }
                        if (nextWait > 10 * SECONDS) {
                            log("Next awake in " + nextWait / 1000 + "s: " + mNextWaitReason);
                            runAsleep(SYNC_MANAGER_ID, nextWait + (3 * SECONDS));
                        }
                        wait(nextWait);
                    }
                }
            } catch (InterruptedException e) {
                // Needs to be caught, but causes no problem
                log("SyncManager interrupted");
            } finally {
                synchronized (this) {
                    if (mKicked) {
                        //log("Wait deferred due to kick");
                        mKicked = false;
                    }
                }
            }
        }
        log("Shutdown requested");
    } catch (RuntimeException e) {
        Log.e(TAG, "RuntimeException in SyncManager", e);
        throw e;
    } finally {
        shutdown();
    }
}

From source file:com.mwebster.exchange.SyncManager.java

public void run() {
    mStop = false;/*from www.  jav  a  2 s . co  m*/

    // If we're really debugging, turn on all logging
    if (Eas.DEBUG) {
        Eas.USER_LOG = true;
        Eas.PARSER_LOG = true;
        Eas.FILE_LOG = true;
    }

    // If we need to wait for the debugger, do so
    if (Eas.WAIT_DEBUG) {
        Debug.waitForDebugger();
    }

    // Set up our observers; we need them to know when to start/stop various syncs based
    // on the insert/delete/update of mailboxes and accounts
    // We also observe synced messages to trigger upsyncs at the appropriate time
    mResolver.registerContentObserver(Mailbox.CONTENT_URI, false, mMailboxObserver);
    mResolver.registerContentObserver(Message.SYNCED_CONTENT_URI, true, mSyncedMessageObserver);
    mResolver.registerContentObserver(Message.CONTENT_URI, true, mMessageObserver);
    ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, mSyncStatusObserver);
    mAccountsUpdatedListener = new EasAccountsUpdatedListener();
    // TODO Find and fix root cause of duplication
    try {
        AccountManager.get(getApplication()).addOnAccountsUpdatedListener(mAccountsUpdatedListener, mHandler,
                true);
    } catch (IllegalStateException e1) {
        // This exception is more of a warning; we shouldn't be in the state in which we
        // already have a listener.
    }

    // Set up receivers for ConnectivityManager
    mConnectivityReceiver = new ConnectivityReceiver();
    registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    mBackgroundDataSettingReceiver = new ConnectivityReceiver();
    registerReceiver(mBackgroundDataSettingReceiver,
            new IntentFilter(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED));
    // Save away background data setting; we'll keep track of it with the receiver
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    mBackgroundData = cm.getBackgroundDataSetting();

    // See if any settings have changed while we weren't running...
    checkPIMSyncSettings();

    try {
        while (!mStop) {
            runAwake(SYNC_MANAGER_ID);
            waitForConnectivity();
            mNextWaitReason = "Heartbeat";
            long nextWait = checkMailboxes();
            try {
                synchronized (this) {
                    if (!mKicked) {
                        if (nextWait < 0) {
                            log("Negative wait? Setting to 1s");
                            nextWait = 1 * SECONDS;
                        }
                        if (nextWait > 10 * SECONDS) {
                            log("Next awake in " + nextWait / 1000 + "s: " + mNextWaitReason);
                            runAsleep(SYNC_MANAGER_ID, nextWait + (3 * SECONDS));
                        }
                        wait(nextWait);
                    }
                }
            } catch (InterruptedException e) {
                // Needs to be caught, but causes no problem
            } finally {
                synchronized (this) {
                    if (mKicked) {
                        //log("Wait deferred due to kick");
                        mKicked = false;
                    }
                }
            }
        }
        log("Shutdown requested");
    } catch (RuntimeException e) {
        Log.e(TAG, "RuntimeException in SyncManager", e);
        throw e;
    } finally {
        log("Finishing SyncManager");
        // Lots of cleanup here
        // Stop our running syncs
        stopServiceThreads();

        // Stop receivers and content observers
        if (mConnectivityReceiver != null) {
            unregisterReceiver(mConnectivityReceiver);
        }
        if (mBackgroundDataSettingReceiver != null) {
            unregisterReceiver(mBackgroundDataSettingReceiver);
        }

        if (INSTANCE != null) {
            ContentResolver resolver = getContentResolver();
            resolver.unregisterContentObserver(mAccountObserver);
            resolver.unregisterContentObserver(mMailboxObserver);
            resolver.unregisterContentObserver(mSyncedMessageObserver);
            resolver.unregisterContentObserver(mMessageObserver);
            unregisterCalendarObservers();
        }
        // Don't leak the Intent associated with this listener
        if (mAccountsUpdatedListener != null) {
            AccountManager.get(this).removeOnAccountsUpdatedListener(mAccountsUpdatedListener);
            mAccountsUpdatedListener = null;
        }

        // Clear pending alarms and associated Intents
        clearAlarms();

        // Release our wake lock, if we have one
        synchronized (mWakeLocks) {
            if (mWakeLock != null) {
                mWakeLock.release();
                mWakeLock = null;
            }
        }

        log("Goodbye");
    }

    if (!mStop) {
        // If this wasn't intentional, try to restart the service
        throw new RuntimeException("EAS SyncManager crash; please restart me...");
    }
}

From source file:com.android.exchange.ExchangeService.java

public void run() {
    sStop = false;//from   w  w  w .  j av a  2 s .co  m
    alwaysLog("ExchangeService thread running");
    // If we're really debugging, turn on all logging
    if (Eas.DEBUG) {
        Eas.USER_LOG = true;
        Eas.PARSER_LOG = true;
        Eas.FILE_LOG = true;
    }

    TempDirectory.setTempDirectory(this);

    // If we need to wait for the debugger, do so
    if (Eas.WAIT_DEBUG) {
        Debug.waitForDebugger();
    }

    // Synchronize here to prevent a shutdown from happening while we initialize our observers
    // and receivers
    synchronized (sSyncLock) {
        if (INSTANCE != null) {
            mResolver = getContentResolver();

            // Set up our observers; we need them to know when to start/stop various syncs based
            // on the insert/delete/update of mailboxes and accounts
            // We also observe synced messages to trigger upsyncs at the appropriate time
            mAccountObserver = new AccountObserver(mHandler);
            mResolver.registerContentObserver(Account.NOTIFIER_URI, true, mAccountObserver);
            mMailboxObserver = new MailboxObserver(mHandler);
            mResolver.registerContentObserver(Mailbox.CONTENT_URI, false, mMailboxObserver);
            mSyncedMessageObserver = new SyncedMessageObserver(mHandler);
            mResolver.registerContentObserver(Message.SYNCED_CONTENT_URI, true, mSyncedMessageObserver);

            // Set up receivers for connectivity and background data setting
            mConnectivityReceiver = new ConnectivityReceiver();
            registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

            mBackgroundDataSettingReceiver = new ConnectivityReceiver();
            registerReceiver(mBackgroundDataSettingReceiver,
                    new IntentFilter(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED));
            // Save away the current background data setting; we'll keep track of it with the
            // receiver we just registered
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            mBackgroundData = cm.getBackgroundDataSetting();

            // Do any required work to clean up our Mailboxes (this serves to upgrade
            // mailboxes that existed prior to EmailProvider database version 17)
            MailboxUtilities.fixupUninitializedParentKeys(this, getEasAccountSelector());
        }
    }

    try {
        // Loop indefinitely until we're shut down
        while (!sStop) {
            runAwake(EXTRA_MAILBOX_ID);
            waitForConnectivity();
            mNextWaitReason = null;
            long nextWait = checkMailboxes();
            try {
                synchronized (this) {
                    if (!mKicked) {
                        if (nextWait < 0) {
                            log("Negative wait? Setting to 1s");
                            nextWait = 1 * SECONDS;
                        }
                        if (nextWait > 10 * SECONDS) {
                            if (mNextWaitReason != null) {
                                log("Next awake " + nextWait / 1000 + "s: " + mNextWaitReason);
                            }
                            runAsleep(EXTRA_MAILBOX_ID, nextWait + (3 * SECONDS));
                        }
                        wait(nextWait);
                    }
                }
            } catch (InterruptedException e) {
                // Needs to be caught, but causes no problem
                log("ExchangeService interrupted");
            } finally {
                synchronized (this) {
                    if (mKicked) {
                        //log("Wait deferred due to kick");
                        mKicked = false;
                    }
                }
            }
        }
        log("Shutdown requested");
    } catch (ProviderUnavailableException pue) {
        // Shutdown cleanly in this case
        // NOTE: Sync adapters will also crash with this error, but that is already handled
        // in the adapters themselves, i.e. they return cleanly via done().  When the Email
        // process starts running again, the Exchange process will be started again in due
        // course, assuming there is at least one existing EAS account.
        Log.e(TAG, "EmailProvider unavailable; shutting down");
        // Ask for our service to be restarted; this should kick-start the Email process as well
        startService(new Intent(this, ExchangeService.class));
    } catch (RuntimeException e) {
        // Crash; this is a completely unexpected runtime error
        Log.e(TAG, "RuntimeException in ExchangeService", e);
        throw e;
    } finally {
        shutdown();
    }
}

From source file:eu.faircode.netguard.ServiceSinkhole.java

@Override
public void onCreate() {
    Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    // Native init
    jni_init();//from w  ww.  ja  v  a 2  s.com
    boolean pcap = prefs.getBoolean("pcap", false);
    setPcap(pcap, this);

    prefs.registerOnSharedPreferenceChangeListener(this);

    Util.setTheme(this);
    super.onCreate();

    HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command",
            Process.THREAD_PRIORITY_FOREGROUND);
    HandlerThread logThread = new HandlerThread(getString(R.string.app_name) + " log",
            Process.THREAD_PRIORITY_BACKGROUND);
    HandlerThread statsThread = new HandlerThread(getString(R.string.app_name) + " stats",
            Process.THREAD_PRIORITY_BACKGROUND);
    commandThread.start();
    logThread.start();
    statsThread.start();

    commandLooper = commandThread.getLooper();
    logLooper = logThread.getLooper();
    statsLooper = statsThread.getLooper();

    commandHandler = new CommandHandler(commandLooper);
    logHandler = new LogHandler(logLooper);
    statsHandler = new StatsHandler(statsLooper);

    // Listen for power save mode
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !Util.isPlayStoreInstall(this)) {
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        powersaving = pm.isPowerSaveMode();
        IntentFilter ifPower = new IntentFilter();
        ifPower.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
        registerReceiver(powerSaveReceiver, ifPower);
        registeredPowerSave = true;
    }

    // Listen for user switches
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        IntentFilter ifUser = new IntentFilter();
        ifUser.addAction(Intent.ACTION_USER_BACKGROUND);
        ifUser.addAction(Intent.ACTION_USER_FOREGROUND);
        registerReceiver(userReceiver, ifUser);
        registeredUser = true;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Listen for idle mode state changes
        IntentFilter ifIdle = new IntentFilter();
        ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
        registerReceiver(idleStateReceiver, ifIdle);
        registeredIdleState = true;
    }

    // Listen for connectivity updates
    IntentFilter ifConnectivity = new IntentFilter();
    ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(connectivityChangedReceiver, ifConnectivity);
    registeredConnectivityChanged = true;

    // Listen for added applications
    IntentFilter ifPackage = new IntentFilter();
    ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
    ifPackage.addAction(Intent.ACTION_PACKAGE_REMOVED);
    ifPackage.addDataScheme("package");
    registerReceiver(packageChangedReceiver, ifPackage);
    registeredPackageChanged = true;

    // Setup house holding
    Intent alarmIntent = new Intent(this, ServiceSinkhole.class);
    alarmIntent.setAction(ACTION_HOUSE_HOLDING);
    PendingIntent pi = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000,
            AlarmManager.INTERVAL_HALF_DAY, pi);
}