Example usage for android.app Service START_FLAG_RETRY

List of usage examples for android.app Service START_FLAG_RETRY

Introduction

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

Prototype

int START_FLAG_RETRY

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

Click Source Link

Document

This flag is set in #onStartCommand if the Intent is a retry because the original attempt never got to or returned from #onStartCommand(Intent,int,int) .

Usage

From source file:com.BeatYourRecord.AssignmentSyncService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    final int startId_ = startId;
    if (!isRunning) {
        isRunning = true;//from   ww  w  .ja v a2s  . c  om
    } else {
        Log.d(LOG_TAG, "sync service is already running, stop and return!!!");
        // For whatever reason, the previous onStartCommand is still running,
        // perhaps the service got
        // killed by runtime and this service is not STICKY, so it never reach end
        // to call stopSelf?
        // So just kill it and let another alarm to start it again.
        stopSelf(startId_);
        return Service.START_NOT_STICKY;
    }

    // Log.d(LOG_TAG, "incoming startId=" + startId_);
    // Log.d(LOG_TAG, "thread id=" + Thread.currentThread().getId());
    // Log.d(LOG_TAG, "thread count=" +
    // Thread.currentThread().getThreadGroup().activeCount());
    if ((flags & Service.START_FLAG_RETRY) == Service.START_FLAG_RETRY) {
        Log.d(LOG_TAG, "assignment service retry");
    }

    ytdDomain = intent.getStringExtra(DbHelper.YTD_DOMAIN);
    ytdJsonRpcUrl = "http://" + ytdDomain + "/jsonrpc";

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            stopSelf(startId_);
            Log.d(LOG_TAG, "startId=" + startId_ + " is finished.");
        }
    };

    new Thread() {
        @Override
        public void run() {
            String newAssignmentId = updateAssignmentDb();
            if (newAssignmentId != null) {
                Intent intent = new Intent();
                intent.setAction(MainActivity.NEW_ASSIGNMENT_UPDATE);
                intent.putExtra(DbHelper.YTD_DOMAIN, ytdDomain);
                intent.putExtra(DbHelper.ASSIGNMENT_ID, newAssignmentId);
                sendBroadcast(intent);
                sendNotification(ytdDomain, newAssignmentId);
            }

            handler.sendEmptyMessage(0);
        }
    }.start();

    return Service.START_NOT_STICKY;
}

From source file:eu.geopaparazzi.library.gps.GpsService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    // GPLog.addLogEntry(this, "onStartCommand called with intent: " + intent);

    /*/*from   www  . jav  a2s. co  m*/
     * If startService(intent) is called while the service is running, 
     * its onStartCommand() is also called. Therefore your service needs 
     * to be prepared that onStartCommand() can be called several times.
     */
    if (preferences == null) {
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        useNetworkPositions = preferences.getBoolean(LibraryConstants.PREFS_KEY_GPS_USE_NETWORK_POSITION,
                false);
        isMockMode = preferences.getBoolean(LibraryConstants.PREFS_KEY_MOCKMODE, false);

        toastHandler = new Handler();
        log("onStartCommand: Preferences created");
    }
    if (locationManager == null) {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return Service.START_FLAG_RETRY;
        }
        locationManager.addGpsStatusListener(this);
        isProviderEnabled = isGpsOn();

        log("onStartCommand: LocationManager created + GpsService started");
    }
    if (!isListeningForUpdates) {
        registerForLocationUpdates();
        log("onStartCommand: Registered for location updates");
    }
    if (intent != null) {
        /*
         * START GPS logging
         */
        if (intent.hasExtra(START_GPS_LOGGING)) {
            boolean startGpsLogging = intent.getBooleanExtra(START_GPS_LOGGING, false);
            if (startGpsLogging) {
                log("onStartCommand: Start GPS logging called");
                if (!isDatabaseLogging) {
                    String gpsLogName = intent.getStringExtra(START_GPS_LOG_NAME);
                    String gpsLogHelperClass = intent.getStringExtra(START_GPS_LOG_HELPER_CLASS);
                    boolean continueLastGpsLog = intent.getBooleanExtra(START_GPS_CONTINUE_LOG, false);
                    try {
                        Class<?> logHelper = Class.forName(gpsLogHelperClass);
                        IGpsLogDbHelper newInstance = (IGpsLogDbHelper) logHelper.newInstance();
                        startDatabaseLogging(gpsLogName, continueLastGpsLog, newInstance);
                    } catch (Exception e) {
                        GPLog.error(this, "Could not start logging", e);
                    }
                }
            }
        }
        if (intent.hasExtra(STOP_GPS_LOGGING)) {
            boolean stopGpsLogging = intent.getBooleanExtra(STOP_GPS_LOGGING, false);
            if (stopGpsLogging) {
                log("onStartCommand: Stop GPS logging called");
                if (isDatabaseLogging) {
                    stopDatabaseLogging();
                }
            }
        }
        if (intent.hasExtra(GPS_SERVICE_DO_BROADCAST)) {
            log("onStartCommand: broadcast trigger");
            boolean doBroadcast = intent.getBooleanExtra(GPS_SERVICE_DO_BROADCAST, false);
            if (doBroadcast) {
                broadcast("triggered by onStartCommand Intent");
            }
        }

    }

    return Service.START_REDELIVER_INTENT;
}