Example usage for android.app Service START_STICKY

List of usage examples for android.app Service START_STICKY

Introduction

In this page you can find the example usage for android.app Service START_STICKY.

Prototype

int START_STICKY

To view the source code for android.app Service START_STICKY.

Click Source Link

Document

Constant to return from #onStartCommand : if this service's process is killed while it is started (after returning from #onStartCommand ), then leave it in the started state but don't retain this delivered intent.

Usage

From source file:com.chen.emailsync.SyncManager.java

@SuppressWarnings("deprecation")
@Override//from w  w w .j  av  a 2  s  .  com
public int onStartCommand(Intent intent, int flags, int startId) {
    alwaysLog("!!! onStartCommand, startingUp = " + sStartingUp + ", running = " + (INSTANCE != null));
    if (!sStartingUp && INSTANCE == null) {
        sStartingUp = true;
        Utility.runAsync(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (sSyncLock) {
                        // SyncServiceManager cannot start unless we connect to AccountService
                        if (!new AccountServiceProxy(SyncManager.this).test()) {
                            alwaysLog("!!! Email application not found; stopping self");
                            stopSelf();
                        }
                        String deviceId = getDeviceId(SyncManager.this);
                        if (deviceId == null) {
                            alwaysLog("!!! deviceId unknown; stopping self and retrying");
                            stopSelf();
                            // Try to restart ourselves in a few seconds
                            Utility.runAsync(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        Thread.sleep(5000);
                                    } catch (InterruptedException e) {
                                    }
                                    startService(getServiceIntent());
                                }
                            });
                            return;
                        }
                        // Run the reconciler and clean up mismatched accounts - if we weren't
                        // running when accounts were deleted, it won't have been called.
                        runAccountReconcilerSync(SyncManager.this);
                        // Update other services depending on final account configuration
                        maybeStartSyncServiceManagerThread();
                        if (sServiceThread == null) {
                            log("!!! EAS SyncServiceManager, stopping self");
                            stopSelf();
                        } else if (sStop) {
                            // If we were trying to stop, attempt a restart in 5 secs
                            setAlarm(SYNC_SERVICE_MAILBOX_ID, 5 * SECONDS);
                        } else {
                            mServiceStartTime = System.currentTimeMillis();
                        }
                    }
                } finally {
                    sStartingUp = false;
                }
            }
        });
    }
    return Service.START_STICKY;
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    synchronized (sSyncLock) {
        alwaysLog("!!! EAS SyncManager, onStartCommand");
        // Restore accounts, if it has not happened already
        AccountBackupRestore.restoreAccountsIfNeeded(this);
        maybeStartSyncManagerThread();/*w  w w. j  a va2 s.  c  o m*/
        if (sServiceThread == null) {
            alwaysLog("!!! EAS SyncManager, stopping self");
            stopSelf();
        }
        return Service.START_STICKY;
    }
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    alwaysLog("!!! EAS ExchangeService, onStartCommand, startingUp = " + sStartingUp + ", running = "
            + (INSTANCE != null));//from w  w  w.  java  2 s  .c o  m
    if (!sStartingUp && INSTANCE == null) {
        sStartingUp = true;
        Utility.runAsync(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (sSyncLock) {
                        // ExchangeService cannot start unless we can connect to AccountService
                        if (!new AccountServiceProxy(ExchangeService.this).test()) {
                            alwaysLog("!!! Email application not found; stopping self");
                            stopSelf();
                        }
                        if (sDeviceId == null) {
                            try {
                                String deviceId = getDeviceId(ExchangeService.this);
                                if (deviceId != null) {
                                    sDeviceId = deviceId;
                                }
                            } catch (IOException e) {
                            }
                            if (sDeviceId == null) {
                                alwaysLog("!!! deviceId unknown; stopping self and retrying");
                                stopSelf();
                                // Try to restart ourselves in a few seconds
                                Utility.runAsync(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            Thread.sleep(5000);
                                        } catch (InterruptedException e) {
                                        }
                                        startService(new Intent(EmailServiceProxy.EXCHANGE_INTENT));
                                    }
                                });
                                return;
                            }
                        }
                        // Run the reconciler and clean up mismatched accounts - if we weren't
                        // running when accounts were deleted, it won't have been called.
                        runAccountReconcilerSync(ExchangeService.this);
                        // Update other services depending on final account configuration
                        maybeStartExchangeServiceThread();
                        if (sServiceThread == null) {
                            log("!!! EAS ExchangeService, stopping self");
                            stopSelf();
                        } else if (sStop) {
                            // If we were trying to stop, attempt a restart in 5 secs
                            setAlarm(EXCHANGE_SERVICE_MAILBOX_ID, 5 * SECONDS);
                        }
                    }
                } finally {
                    sStartingUp = false;
                }
            }
        });
    }
    return Service.START_STICKY;
}