Example usage for android.app Service START_NOT_STICKY

List of usage examples for android.app Service START_NOT_STICKY

Introduction

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

Prototype

int START_NOT_STICKY

To view the source code for android.app Service START_NOT_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 ), and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate until a future explicit call to Context#startService Context.startService(Intent) .

Usage

From source file:com.digitalarx.android.files.services.FileUploader.java

/**
 * Entry point to add one or several files to the queue of uploads.
 * //w w  w. j  a va 2s .  co  m
 * New uploads are added calling to startService(), resulting in a call to
 * this method. This ensures the service will keep on working although the
 * caller activity goes away.
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_UPLOAD_TYPE)
            || !(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
        Log_OC.e(TAG, "Not enough information provided in intent");
        return Service.START_NOT_STICKY;
    }
    int uploadType = intent.getIntExtra(KEY_UPLOAD_TYPE, -1);
    if (uploadType == -1) {
        Log_OC.e(TAG, "Incorrect upload type provided");
        return Service.START_NOT_STICKY;
    }
    Account account = intent.getParcelableExtra(KEY_ACCOUNT);

    String[] localPaths = null, remotePaths = null, mimeTypes = null;
    OCFile[] files = null;
    if (uploadType == UPLOAD_SINGLE_FILE) {

        if (intent.hasExtra(KEY_FILE)) {
            files = new OCFile[] { intent.getParcelableExtra(KEY_FILE) };

        } else {
            localPaths = new String[] { intent.getStringExtra(KEY_LOCAL_FILE) };
            remotePaths = new String[] { intent.getStringExtra(KEY_REMOTE_FILE) };
            mimeTypes = new String[] { intent.getStringExtra(KEY_MIME_TYPE) };
        }

    } else { // mUploadType == UPLOAD_MULTIPLE_FILES

        if (intent.hasExtra(KEY_FILE)) {
            files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE); // TODO
                                                                         // will
                                                                         // this
                                                                         // casting
                                                                         // work
                                                                         // fine?

        } else {
            localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
            remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
            mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
        }
    }

    FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver());

    boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
    boolean isInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false);
    int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_COPY);

    if (intent.hasExtra(KEY_FILE) && files == null) {
        Log_OC.e(TAG, "Incorrect array for OCFiles provided in upload intent");
        return Service.START_NOT_STICKY;

    } else if (!intent.hasExtra(KEY_FILE)) {
        if (localPaths == null) {
            Log_OC.e(TAG, "Incorrect array for local paths provided in upload intent");
            return Service.START_NOT_STICKY;
        }
        if (remotePaths == null) {
            Log_OC.e(TAG, "Incorrect array for remote paths provided in upload intent");
            return Service.START_NOT_STICKY;
        }
        if (localPaths.length != remotePaths.length) {
            Log_OC.e(TAG, "Different number of remote paths and local paths!");
            return Service.START_NOT_STICKY;
        }

        files = new OCFile[localPaths.length];
        for (int i = 0; i < localPaths.length; i++) {
            files[i] = obtainNewOCFileToUpload(remotePaths[i], localPaths[i],
                    ((mimeTypes != null) ? mimeTypes[i] : (String) null), storageManager);
            if (files[i] == null) {
                // TODO @andomaex add failure Notification
                return Service.START_NOT_STICKY;
            }
        }
    }

    AccountManager aMgr = AccountManager.get(this);
    String version = aMgr.getUserData(account, Constants.KEY_OC_VERSION);
    OwnCloudVersion ocv = new OwnCloudVersion(version);

    boolean chunked = FileUploader.chunkedUploadIsSupported(ocv);
    AbstractList<String> requestedUploads = new Vector<String>();
    String uploadKey = null;
    UploadFileOperation newUpload = null;
    try {
        for (int i = 0; i < files.length; i++) {
            uploadKey = buildRemoteName(account, files[i].getRemotePath());
            newUpload = new UploadFileOperation(account, files[i], chunked, isInstant, forceOverwrite,
                    localAction, getApplicationContext());
            if (isInstant) {
                newUpload.setRemoteFolderToBeCreated();
            }
            mPendingUploads.putIfAbsent(uploadKey, newUpload); // Grants that the file only upload once time

            newUpload.addDatatransferProgressListener(this);
            newUpload.addDatatransferProgressListener((FileUploaderBinder) mBinder);
            requestedUploads.add(uploadKey);
        }

    } catch (IllegalArgumentException e) {
        Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
        return START_NOT_STICKY;

    } catch (IllegalStateException e) {
        Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
        return START_NOT_STICKY;

    } catch (Exception e) {
        Log_OC.e(TAG, "Unexpected exception while processing upload intent", e);
        return START_NOT_STICKY;

    }

    if (requestedUploads.size() > 0) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = requestedUploads;
        mServiceHandler.sendMessage(msg);
    }
    Log_OC.i(TAG, "mPendingUploads size:" + mPendingUploads.size());
    return Service.START_NOT_STICKY;
}

From source file:com.owncloud.android.files.services.FileUploader.java

/**
 * Entry point to add one or several files to the queue of uploads.
 *
 * New uploads are added calling to startService(), resulting in a call to
 * this method. This ensures the service will keep on working although the
 * caller activity goes away./*from  www.  jav a 2s  .c om*/
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log_OC.d(TAG, "Starting command with id " + startId);

    if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_UPLOAD_TYPE)
            || !(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
        Log_OC.e(TAG, "Not enough information provided in intent");
        return Service.START_NOT_STICKY;
    }
    int uploadType = intent.getIntExtra(KEY_UPLOAD_TYPE, -1);
    if (uploadType == -1) {
        Log_OC.e(TAG, "Incorrect upload type provided");
        return Service.START_NOT_STICKY;
    }
    Account account = intent.getParcelableExtra(KEY_ACCOUNT);
    if (!AccountUtils.exists(account, getApplicationContext())) {
        return Service.START_NOT_STICKY;
    }

    String[] localPaths = null, remotePaths = null, mimeTypes = null;
    OCFile[] files = null;
    if (uploadType == UPLOAD_SINGLE_FILE) {

        if (intent.hasExtra(KEY_FILE)) {
            files = new OCFile[] { intent.getParcelableExtra(KEY_FILE) };

        } else {
            localPaths = new String[] { intent.getStringExtra(KEY_LOCAL_FILE) };
            remotePaths = new String[] { intent.getStringExtra(KEY_REMOTE_FILE) };
            mimeTypes = new String[] { intent.getStringExtra(KEY_MIME_TYPE) };
        }

    } else { // mUploadType == UPLOAD_MULTIPLE_FILES

        if (intent.hasExtra(KEY_FILE)) {
            files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE); // TODO
                                                                         // will
                                                                         // this
                                                                         // casting
                                                                         // work
                                                                         // fine?

        } else {
            localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
            remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
            mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
        }
    }

    FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver());

    boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
    boolean isInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false);
    int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_COPY);

    if (intent.hasExtra(KEY_FILE) && files == null) {
        Log_OC.e(TAG, "Incorrect array for OCFiles provided in upload intent");
        return Service.START_NOT_STICKY;

    } else if (!intent.hasExtra(KEY_FILE)) {
        if (localPaths == null) {
            Log_OC.e(TAG, "Incorrect array for local paths provided in upload intent");
            return Service.START_NOT_STICKY;
        }
        if (remotePaths == null) {
            Log_OC.e(TAG, "Incorrect array for remote paths provided in upload intent");
            return Service.START_NOT_STICKY;
        }
        if (localPaths.length != remotePaths.length) {
            Log_OC.e(TAG, "Different number of remote paths and local paths!");
            return Service.START_NOT_STICKY;
        }

        files = new OCFile[localPaths.length];
        for (int i = 0; i < localPaths.length; i++) {
            files[i] = obtainNewOCFileToUpload(remotePaths[i], localPaths[i],
                    ((mimeTypes != null) ? mimeTypes[i] : null));
            if (files[i] == null) {
                // TODO @andomaex add failure Notification
                return Service.START_NOT_STICKY;
            }
        }
    }

    OwnCloudVersion ocv = AccountUtils.getServerVersion(account);

    boolean chunked = FileUploader.chunkedUploadIsSupported(ocv);
    AbstractList<String> requestedUploads = new Vector<String>();
    String uploadKey = null;
    UploadFileOperation newUpload = null;
    try {
        for (int i = 0; i < files.length; i++) {
            newUpload = new UploadFileOperation(account, files[i], chunked, isInstant, forceOverwrite,
                    localAction, getApplicationContext());
            if (isInstant) {
                newUpload.setRemoteFolderToBeCreated();
            }
            newUpload.addDatatransferProgressListener(this);
            newUpload.addDatatransferProgressListener((FileUploaderBinder) mBinder);
            Pair<String, String> putResult = mPendingUploads.putIfAbsent(account, files[i].getRemotePath(),
                    newUpload);
            uploadKey = putResult.first;
            requestedUploads.add(uploadKey);
        }

    } catch (IllegalArgumentException e) {
        Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
        return START_NOT_STICKY;

    } catch (IllegalStateException e) {
        Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
        return START_NOT_STICKY;

    } catch (Exception e) {
        Log_OC.e(TAG, "Unexpected exception while processing upload intent", e);
        return START_NOT_STICKY;

    }

    if (requestedUploads.size() > 0) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = requestedUploads;
        mServiceHandler.sendMessage(msg);
    }
    return Service.START_NOT_STICKY;
}

From source file:com.cerema.cloud2.files.services.FileUploader.java

/**
 * Entry point to add one or several files to the queue of uploads.
 *
 * New uploads are added calling to startService(), resulting in a call to
 * this method. This ensures the service will keep on working although the
 * caller activity goes away./*from   www.j  a va 2s  .c  om*/
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log_OC.d(TAG, "Starting command with id " + startId);

    if (intent.hasExtra(KEY_CANCEL_ALL) && intent.hasExtra(KEY_ACCOUNT)) {
        Account account = intent.getParcelableExtra(KEY_ACCOUNT);

        if (mCurrentUpload != null) {
            FileUploaderBinder fub = (FileUploaderBinder) mBinder;
            fub.cancel(account);
            return Service.START_NOT_STICKY;
        }
    }

    if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_UPLOAD_TYPE)
            || !(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
        Log_OC.e(TAG, "Not enough information provided in intent");
        return Service.START_NOT_STICKY;
    }
    int uploadType = intent.getIntExtra(KEY_UPLOAD_TYPE, -1);
    if (uploadType == -1) {
        Log_OC.e(TAG, "Incorrect upload type provided");
        return Service.START_NOT_STICKY;
    }
    Account account = intent.getParcelableExtra(KEY_ACCOUNT);
    if (!AccountUtils.exists(account, getApplicationContext())) {
        return Service.START_NOT_STICKY;
    }

    String[] localPaths = null, remotePaths = null, mimeTypes = null;
    OCFile[] files = null;
    if (uploadType == UPLOAD_SINGLE_FILE) {

        if (intent.hasExtra(KEY_FILE)) {
            files = new OCFile[] { intent.getParcelableExtra(KEY_FILE) };

        } else {
            localPaths = new String[] { intent.getStringExtra(KEY_LOCAL_FILE) };
            remotePaths = new String[] { intent.getStringExtra(KEY_REMOTE_FILE) };
            mimeTypes = new String[] { intent.getStringExtra(KEY_MIME_TYPE) };
        }

    } else { // mUploadType == UPLOAD_MULTIPLE_FILES

        if (intent.hasExtra(KEY_FILE)) {
            files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE); // TODO
                                                                         // will
                                                                         // this
                                                                         // casting
                                                                         // work
                                                                         // fine?

        } else {
            localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
            remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
            mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
        }
    }

    FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver());

    boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
    boolean isInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false);
    int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_FORGET);

    if (intent.hasExtra(KEY_FILE) && files == null) {
        Log_OC.e(TAG, "Incorrect array for OCFiles provided in upload intent");
        return Service.START_NOT_STICKY;

    } else if (!intent.hasExtra(KEY_FILE)) {
        if (localPaths == null) {
            Log_OC.e(TAG, "Incorrect array for local paths provided in upload intent");
            return Service.START_NOT_STICKY;
        }
        if (remotePaths == null) {
            Log_OC.e(TAG, "Incorrect array for remote paths provided in upload intent");
            return Service.START_NOT_STICKY;
        }
        if (localPaths.length != remotePaths.length) {
            Log_OC.e(TAG, "Different number of remote paths and local paths!");
            return Service.START_NOT_STICKY;
        }

        files = new OCFile[localPaths.length];
        for (int i = 0; i < localPaths.length; i++) {
            files[i] = obtainNewOCFileToUpload(remotePaths[i], localPaths[i],
                    ((mimeTypes != null) ? mimeTypes[i] : null));
            if (files[i] == null) {
                // TODO @andomaex add failure Notification
                return Service.START_NOT_STICKY;
            }
        }
    }

    OwnCloudVersion ocv = AccountUtils.getServerVersion(account);

    boolean chunked = FileUploader.chunkedUploadIsSupported(ocv);
    AbstractList<String> requestedUploads = new Vector<String>();
    String uploadKey = null;
    UploadFileOperation newUpload = null;
    try {
        for (int i = 0; i < files.length; i++) {
            newUpload = new UploadFileOperation(account, files[i], chunked, isInstant, forceOverwrite,
                    localAction, getApplicationContext());
            if (isInstant) {
                newUpload.setRemoteFolderToBeCreated();
            }
            newUpload.addDatatransferProgressListener(this);
            newUpload.addDatatransferProgressListener((FileUploaderBinder) mBinder);
            Pair<String, String> putResult = mPendingUploads.putIfAbsent(account, files[i].getRemotePath(),
                    newUpload);
            if (putResult != null) {
                uploadKey = putResult.first;
                requestedUploads.add(uploadKey);
            } // else, file already in the queue of uploads; don't repeat the request
        }

    } catch (IllegalArgumentException e) {
        Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
        return START_NOT_STICKY;

    } catch (IllegalStateException e) {
        Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
        return START_NOT_STICKY;

    } catch (Exception e) {
        Log_OC.e(TAG, "Unexpected exception while processing upload intent", e);
        return START_NOT_STICKY;

    }

    if (requestedUploads.size() > 0) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = requestedUploads;
        mServiceHandler.sendMessage(msg);
    }
    return Service.START_NOT_STICKY;
}

From source file:com.synox.android.files.services.FileUploader.java

/**
 * Entry point to add one or several files to the queue of uploads.
 *
 * New uploads are added calling to startService(), resulting in a call to
 * this method. This ensures the service will keep on working although the
 * caller activity goes away./*ww  w . j  a  v a  2  s .  com*/
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log_OC.d(TAG, "Starting command with id " + startId);

    if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_UPLOAD_TYPE)
            || !(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
        Log_OC.e(TAG, "Not enough information provided in intent");
        return Service.START_NOT_STICKY;
    }
    int uploadType = intent.getIntExtra(KEY_UPLOAD_TYPE, -1);
    if (uploadType == -1) {
        Log_OC.e(TAG, "Incorrect upload type provided");
        return Service.START_NOT_STICKY;
    }
    Account account = intent.getParcelableExtra(KEY_ACCOUNT);
    if (!AccountUtils.exists(account, getApplicationContext())) {
        return Service.START_NOT_STICKY;
    }

    String[] localPaths = null, remotePaths = null, mimeTypes = null;
    OCFile[] files = null;
    if (uploadType == UPLOAD_SINGLE_FILE) {

        if (intent.hasExtra(KEY_FILE)) {
            files = new OCFile[] { intent.getParcelableExtra(KEY_FILE) };

        } else {
            localPaths = new String[] { intent.getStringExtra(KEY_LOCAL_FILE) };
            remotePaths = new String[] { intent.getStringExtra(KEY_REMOTE_FILE) };
            mimeTypes = new String[] { intent.getStringExtra(KEY_MIME_TYPE) };
        }

    } else { // mUploadType == UPLOAD_MULTIPLE_FILES

        if (intent.hasExtra(KEY_FILE)) {
            files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE); // TODO
                                                                         // will
                                                                         // this
                                                                         // casting
                                                                         // work
                                                                         // fine?

        } else {
            localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
            remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
            mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
        }
    }

    FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver());

    boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
    boolean isInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false);
    int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_COPY);

    if (intent.hasExtra(KEY_FILE) && files == null) {
        Log_OC.e(TAG, "Incorrect array for OCFiles provided in upload intent");
        return Service.START_NOT_STICKY;

    } else if (!intent.hasExtra(KEY_FILE)) {
        if (localPaths == null) {
            Log_OC.e(TAG, "Incorrect array for local paths provided in upload intent");
            return Service.START_NOT_STICKY;
        }
        if (remotePaths == null) {
            Log_OC.e(TAG, "Incorrect array for remote paths provided in upload intent");
            return Service.START_NOT_STICKY;
        }
        if (localPaths.length != remotePaths.length) {
            Log_OC.e(TAG, "Different number of remote paths and local paths!");
            return Service.START_NOT_STICKY;
        }

        files = new OCFile[localPaths.length];
        for (int i = 0; i < localPaths.length; i++) {
            files[i] = obtainNewOCFileToUpload(remotePaths[i], localPaths[i],
                    ((mimeTypes != null) ? mimeTypes[i] : null), storageManager);
            if (files[i] == null) {
                // TODO @andomaex add failure Notification
                return Service.START_NOT_STICKY;
            }
        }
    }

    OwnCloudVersion ocv = AccountUtils.getServerVersion(account);

    boolean chunked = FileUploader.chunkedUploadIsSupported(ocv);
    AbstractList<String> requestedUploads = new Vector<>();
    String uploadKey = null;
    UploadFileOperation newUpload = null;
    try {
        for (OCFile file : files) {
            uploadKey = buildRemoteName(account, file.getRemotePath());
            newUpload = new UploadFileOperation(account, file, chunked, isInstant, forceOverwrite, localAction,
                    getApplicationContext());
            if (isInstant) {
                newUpload.setRemoteFolderToBeCreated();
            }
            // Grants that the file only upload once time
            mPendingUploads.putIfAbsent(uploadKey, newUpload);

            newUpload.addDatatransferProgressListener(this);
            newUpload.addDatatransferProgressListener((FileUploaderBinder) mBinder);
            requestedUploads.add(uploadKey);
        }

    } catch (IllegalArgumentException e) {
        Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
        return START_NOT_STICKY;

    } catch (IllegalStateException e) {
        Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
        return START_NOT_STICKY;

    } catch (Exception e) {
        Log_OC.e(TAG, "Unexpected exception while processing upload intent", e);
        return START_NOT_STICKY;

    }

    if (requestedUploads.size() > 0) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = requestedUploads;
        mServiceHandler.sendMessage(msg);
    }
    Log_OC.i(TAG, "mPendingUploads size:" + mPendingUploads.size());
    return Service.START_NOT_STICKY;
}

From source file:org.broeuschmeul.android.gps.usb.provider.driver.USBGpsProviderService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor edit = sharedPreferences.edit();

    debugToasts = sharedPreferences.getBoolean(PREF_TOAST_LOGGING, false);

    int vendorId = sharedPreferences.getInt(PREF_GPS_DEVICE_VENDOR_ID,
            USBGpsSettingsFragment.DEFAULT_GPS_VENDOR_ID);
    int productId = sharedPreferences.getInt(PREF_GPS_DEVICE_PRODUCT_ID,
            USBGpsSettingsFragment.DEFAULT_GPS_PRODUCT_ID);

    int maxConRetries = Integer.parseInt(sharedPreferences.getString(PREF_CONNECTION_RETRIES,
            this.getString(R.string.defaultConnectionRetries)));

    log("prefs device addr: " + vendorId + " - " + productId);

    if (ACTION_START_GPS_PROVIDER.equals(intent.getAction())) {
        if (gpsManager == null) {
            String mockProvider = LocationManager.GPS_PROVIDER;
            if (!sharedPreferences.getBoolean(PREF_REPLACE_STD_GPS, true)) {
                mockProvider = sharedPreferences.getString(PREF_MOCK_GPS_NAME,
                        getString(R.string.defaultMockGpsName));
            }/*from   w  w w  .j a  va  2s. c o  m*/
            gpsManager = new USBGpsManager(this, vendorId, productId, maxConRetries);
            boolean enabled = gpsManager.enable();

            if (sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, false) != enabled) {
                edit.putBoolean(PREF_START_GPS_PROVIDER, enabled);
                edit.apply();
            }

            if (enabled) {
                gpsManager.enableMockLocationProvider(mockProvider);

                PendingIntent launchIntent = PendingIntent.getActivity(this, 0,
                        new Intent(this, GpsInfoActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);

                sharedPreferences.edit().putInt(getString(R.string.pref_disable_reason_key), 0).apply();

                Notification notification = new NotificationCompat.Builder(this).setContentIntent(launchIntent)
                        .setSmallIcon(R.drawable.ic_stat_notify).setAutoCancel(true)
                        .setContentTitle(getString(R.string.foreground_service_started_notification_title))
                        .setContentText(getString(R.string.foreground_gps_provider_started_notification))
                        .build();

                startForeground(R.string.foreground_gps_provider_started_notification, notification);

                showToast(R.string.msg_gps_provider_started);

                if (sharedPreferences.getBoolean(PREF_TRACK_RECORDING, false)) {
                    startTracking();
                }
            } else {
                stopSelf();
            }

        } else {
            // We received a start intent even though it's already running so restart
            stopSelf();
            startService(new Intent(this, USBGpsProviderService.class).setAction(intent.getAction()));
        }

    } else if (ACTION_START_TRACK_RECORDING.equals(intent.getAction())) {
        startTracking();

    } else if (ACTION_STOP_TRACK_RECORDING.equals(intent.getAction())) {
        if (gpsManager != null) {
            gpsManager.removeNmeaListener(this);
            endTrack();
            showToast(this.getString(R.string.msg_nmea_recording_stopped));
        }
        if (sharedPreferences.getBoolean(PREF_TRACK_RECORDING, true)) {
            edit.putBoolean(PREF_TRACK_RECORDING, false);
            edit.commit();
        }

    } else if (ACTION_STOP_GPS_PROVIDER.equals(intent.getAction())) {
        if (sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, true)) {
            edit.putBoolean(PREF_START_GPS_PROVIDER, false);
            edit.commit();
        }
        stopSelf();

    } else if (ACTION_CONFIGURE_SIRF_GPS.equals(intent.getAction())
            || ACTION_ENABLE_SIRF_GPS.equals(intent.getAction())) {
        if (gpsManager != null) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                gpsManager.enableSirfConfig(extras);
            } else {
                gpsManager.enableSirfConfig(sharedPreferences);
            }
        }
    }
    return Service.START_NOT_STICKY;
}