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:step.StepService.java

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

    if (Build.VERSION.SDK_INT < 18) {

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.step_launcher).setContentTitle("??")
                .setContentText("??");

        Notification notification = mBuilder.build();

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, StepCountActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

        notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        mBuilder.setContentIntent(contentIntent);
        startForeground(GRAY_SERVICE_ID, notification);//API < 18 ??Notification
    } else {/* w ww.  j  a  v  a 2  s  .  c o  m*/
        Intent innerIntent = new Intent(this, GrayInnerService.class);
        startService(innerIntent);
        startForeground(GRAY_SERVICE_ID, new Notification());
    }

    return Service.START_STICKY;
}

From source file:com.makotosan.vimeodroid.TransferService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    counter++;/* w  w  w  .j a  v  a2s  .  c  om*/
    transfer = new Transfer();
    final String videoUri = intent.getStringExtra("videouri");
    final TransferType transferType = TransferType.valueOf(intent.getStringExtra("transferType"));
    final String fileName = intent.getStringExtra("fileName");
    Bitmap bitmapThumbnail = null;
    int transferIcon = android.R.drawable.stat_sys_download;

    if (transferType == TransferType.Upload) {
        // Get the resource ID for the video
        final long resourceId = ContentUris.parseId(Uri.parse(videoUri));

        bitmapThumbnail = android.provider.MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(),
                resourceId, android.provider.MediaStore.Video.Thumbnails.MICRO_KIND, null);
        transfer.setIcon(bitmapThumbnail);
        transferIcon = android.R.drawable.stat_sys_upload;
    }

    // Initialize our notification
    final Notification notification = new Notification(transferIcon, "Transferring...",
            System.currentTimeMillis());
    notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
    notification.contentView = new RemoteViews(this.getPackageName(), R.layout.transferprogress);

    final Intent manageIntent = new Intent(this, ManageTransfersActivity.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, manageIntent, 0);

    notification.contentIntent = pendingIntent;
    if (bitmapThumbnail != null) {
        notification.contentView.setImageViewBitmap(R.id.transferprogressIcon, bitmapThumbnail);
    } else {
        notification.contentView.setImageViewResource(R.id.transferprogressIcon, R.drawable.icon);
    }

    notification.contentView.setProgressBar(R.id.transferprogressBar, 100, 0, false);
    notification.contentView.setTextViewText(R.id.transferprogressText, "Transferring... ");
    notification.contentView.setOnClickPendingIntent(R.layout.transferprogress, pendingIntent);

    // Add it to the collection of notifications
    notifications.put(counter, notification);

    // Add our notification to the notification tray
    notificationManager.notify(counter, notification);

    // Initialize our asynchronous transfer task
    final TransferTask task = new TransferTask(counter, transferType);

    task.execute(videoUri, fileName);
    return Service.START_STICKY;
}

From source file:com.adam.aslfms.service.ScrobblingService.java

@Override
public int onStartCommand(Intent i, int flags, int startId) {
    handleCommand(i, startId);/* w  w w  . j  a v a  2  s.  c o m*/

    String ar = "";
    String tr = "";
    String api = "";
    if (mCurrentTrack != null) {
        ar = mCurrentTrack.getArtist();
        tr = mCurrentTrack.getTrack();
        api = mCurrentTrack.getMusicAPI().readAPIname();
    }

    Intent targetIntent = new Intent(mCtx, SettingsActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(mCtx, 0, targetIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(mCtx).setContentTitle(ar)
            .setSmallIcon(R.mipmap.ic_notify).setContentText(tr + " : " + api).setContentIntent(contentIntent);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR2) {
        builder.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher));
    }

    this.startForeground(14619, builder.build());

    if (!settings.isNotifyEnabled(Util.checkPower(mCtx))) {
        Intent iNoti = new Intent(mCtx, ForegroundHide.class);
        this.startService(iNoti);
    }
    return Service.START_STICKY;
}

From source file:com.darshancomputing.alockblock.ALockBlockService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getStringExtra(EXTRA_ACTION);

        if (ACTION_REENABLE.equals(action)) {
            SharedPreferences.Editor editor = sp_store.edit();
            editor.putBoolean(ALockBlockService.KEY_DISABLE_LOCKING, false);
            editor.commit();// www  .ja  v  a  2 s .  c  om
        } else if (ACTION_DISABLE.equals(action)) {
            SharedPreferences.Editor editor = sp_store.edit();
            editor.putBoolean(ALockBlockService.KEY_DISABLE_LOCKING, true);
            editor.commit();
        }
    }

    reloadSettings(false);

    return Service.START_STICKY;
}

From source file:net.mceoin.cominghome.LocationService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (debug)//ww  w.ja v  a  2  s  .  c  o  m
        Log.d(TAG, "onStartCommand()");
    startBackgroundTask();
    mLocationClient = new LocationClient(getApplicationContext(), this, this);
    //        mLocationClient.connect();
    LocalBroadcastManager.getInstance(this).registerReceiver(mTrackingReceiver, new IntentFilter(TRACKING));
    trackingETA = true;
    return Service.START_STICKY;
}

From source file:de.androidbytes.adbconnect.presentation.services.WirelessAdbManagingService.java

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

    String intentAction = "no";

    if (intent != null) {
        intentAction = intent.getAction();

        if (intentAction != null) {

            if (intentAction.equals(getString(R.string.service_action_network_not_connected))
                    || intentAction.equals(getString(R.string.service_action_network_connected))) {
                checkAdbConnectRequirements();
                setTurnedOffManually(false);
            } else if (intentAction.equals(getString(R.string.service_action_start_adb_over_wifi))) {
                toggleWirelessAdbState(true);
            } else if (intentAction.equals(getString(R.string.service_action_stop_adb_over_wifi))) {
                toggleWirelessAdbState(false);
            }// ww  w .  ja v a 2s  .co m

        } else {
            intentAction = "no";
        }
    }

    Log.i("WirelessAdbManagingService with " + intentAction + " Action Called");

    return Service.START_STICKY;

}

From source file:com.mb.android.playbackmediator.notification.VideoCastNotificationService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    LOGD(TAG, "onStartCommand");
    if (null != intent) {

        String action = intent.getAction();
        if (ACTION_TOGGLE_PLAYBACK.equals(action) && mIsIcsOrAbove) {
            LOGD(TAG, "onStartCommand(): Action: ACTION_TOGGLE_PLAYBACK");
            togglePlayback();/*ww w. j  a v a  2  s .  co m*/
        } else if (ACTION_STOP.equals(action) && mIsIcsOrAbove) {
            LOGD(TAG, "onStartCommand(): Action: ACTION_STOP");
            stopApplication();
        } else if (ACTION_VISIBILITY.equals(action)) {
            mVisible = intent.getBooleanExtra(NOTIFICATION_VISIBILITY, false);
            LOGD(TAG, "onStartCommand(): Action: ACTION_VISIBILITY " + mVisible);
            if (mVisible && null != mNotification) {
                startForeground(NOTIFICATION_ID, mNotification);
                mCastManager.setContext(this);
            } else {
                stopForeground(true);
            }
        } else {
            LOGD(TAG, "onStartCommand(): Action: none");
        }

    } else {
        LOGD(TAG, "onStartCommand(): Intent was null");
    }

    return Service.START_STICKY;
}

From source file:com.location.philippweiher.test.SendMockLocationService.java

@Override
public int onStartCommand(Intent startIntent, int flags, int startId) {
    // Get the type of test to run
    mTestRequest = startIntent.getAction();

    latitude = startIntent.getDoubleExtra(MapsActivity.LAT_FOR_POINT, 0);
    longitude = startIntent.getDoubleExtra(MapsActivity.LON_FOR_POINT, 0);

    /*//from  ww w  .j  a  v a2 s .c  om
     * If the incoming Intent was a request to run a one-time or continuous test
     */
    if (TextUtils.equals(mTestRequest, MapsActivity.ACTION_START)) {

        // Get the pause interval and injection interval
        mPauseInterval = startIntent.getIntExtra(MapsActivity.EXTRA_PAUSE_VALUE, 2);
        mInjectionInterval = startIntent.getIntExtra(MapsActivity.EXTRA_SEND_INTERVAL, 1);

        // Create a location client
        mLocationClient = new LocationClient(this, this, this);

        // Start connecting to Location Services
        mLocationClient.connect();

    } else if (TextUtils.equals(mTestRequest, MapsActivity.ACTION_STOP_TEST)) {
        // Send a message back to the main activity that the test is stopping
        sendBroadcastMessage();

        // Stop this Service
        stopSelf();
    }

    /*
     * Tell the system to keep the Service alive, but to discard the Intent that
     * started the Service
     */
    return Service.START_STICKY;
}

From source file:com.google.sample.castcompanionlibrary.notification.VideoCastNotificationService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    LOGD(TAG, "onStartCommand");
    if (null != intent) {

        String action = intent.getAction();
        if (ACTION_TOGGLE_PLAYBACK.equals(action) && mIsIcsOrAbove) {
            LOGD(TAG, "onStartCommand(): Action: ACTION_TOGGLE_PLAYBACK");
            togglePlayback();//ww w  .jav  a 2 s  .  co m
        } else if (ACTION_STOP.equals(action) && mIsIcsOrAbove) {
            LOGD(TAG, "onStartCommand(): Action: ACTION_STOP");
            stopApplication();
        } else if (ACTION_VISIBILITY.equals(action)) {
            mVisible = intent.getBooleanExtra(NOTIFICATION_VISIBILITY, false);
            LOGD(TAG, "onStartCommand(): Action: ACTION_VISIBILITY " + mVisible);
            if (mVisible) {
                if (mNotification != null) {
                    startForeground(NOTIFICATION_ID, mNotification);
                } else {
                    try {
                        setupNotification(mCastManager.getRemoteMediaInformation());
                    } catch (TransientNetworkDisconnectionException e) {
                        LOGE(TAG, "onStartCommand() failed to get media", e);
                    } catch (NoConnectionException e) {
                        LOGE(TAG, "onStartCommand() failed to get media", e);
                    }
                }
            } else {
                stopForeground(true);
            }
        } else {
            LOGD(TAG, "onStartCommand(): Action: none");
        }

    } else {
        LOGD(TAG, "onStartCommand(): Intent was null");
    }

    return Service.START_STICKY;
}

From source file:com.owncloud.android.services.observer.FileObserverService.java

/**
 * Handles requests to:/*from   w w  w.j av a 2  s.  c om*/
 *  - (re)start watching                    (ACTION_START_OBSERVE)
 *  - add an {@link OCFile} to be watched   (ATION_ADD_OBSERVED_FILE)
 *  - stop observing an {@link OCFile}      (ACTION_DEL_OBSERVED_FILE) 
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log_OC.d(TAG, "Starting command " + intent);

    if (intent == null || ACTION_START_OBSERVE.equals(intent.getAction())) {
        // NULL occurs when system tries to restart the service after its
        // process was killed
        startObservation();
        return Service.START_STICKY;

    } else if (ACTION_ADD_OBSERVED_FILE.equals(intent.getAction())) {
        OCFile file = intent.getParcelableExtra(ARG_FILE);
        Account account = intent.getParcelableExtra(ARG_ACCOUNT);
        addObservedFile(file, account);

    } else if (ACTION_DEL_OBSERVED_FILE.equals(intent.getAction())) {
        removeObservedFile((OCFile) intent.getParcelableExtra(ARG_FILE),
                (Account) intent.getParcelableExtra(ARG_ACCOUNT));

    } else if (ACTION_UPDATE_AUTO_UPLOAD_OBSERVERS.equals(intent.getAction())) {
        updateInstantUploadsObservers();

    } else {
        Log_OC.e(TAG, "Unknown action received; ignoring it: " + intent.getAction());
    }

    return Service.START_STICKY;
}