Example usage for android.content Context startService

List of usage examples for android.content Context startService

Introduction

In this page you can find the example usage for android.content Context startService.

Prototype

@Nullable
public abstract ComponentName startService(Intent service);

Source Link

Document

Request that a given application service be started.

Usage

From source file:org.y20k.transistor.helpers.SleepTimerService.java

public void startActionStart(Context context, long duration) {
    LogHelper.v(LOG_TAG, "Starting sleep timer. Duration: " + duration);

    // start sleep timer service using intent
    Intent intent = new Intent(context, SleepTimerService.class);
    intent.setAction(TransistorKeys.ACTION_TIMER_START);
    intent.putExtra(TransistorKeys.EXTRA_TIMER_DURATION, duration);
    context.startService(intent);
}

From source file:com.frostwire.android.gui.services.EngineService.java

@Override
public void shutdown() {
    LOG.info("shutdown");

    Context ctx = getApplication();
    Intent i = new Intent(ctx, EngineService.class);
    i.setAction(SHUTDOWN_ACTION);//from  w  w w  .  ja  va  2s.co  m
    ctx.startService(i);
}

From source file:fr.shywim.antoinedaniel.gcm.GcmBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    String msg = intent.getStringExtra("msg");
    if (msg != null && msg.contains("update:")) {
        startWakefulService(context, intent.setComponent(comp));
        setResultCode(Activity.RESULT_OK);
    } else {/*  ww  w.j a va2s  . c  om*/
        context.startService(intent.setComponent(comp));
        setResultCode(Activity.RESULT_OK);
    }
}

From source file:org.metawatch.manager.MetaWatchService.java

public static void sentBytes(Context context, byte[] bytes) {
    if (!MetaWatchStatus.mShutdownRequested && mIsRunning) {
        Intent intent = new Intent(context, MetaWatchService.class);
        intent.putExtra(MetaWatchService.COMMAND_KEY, MetaWatchService.SEND_BYTE_ARRAY);
        intent.putExtra(MetaWatchService.BYTE_ARRAY, bytes);
        context.startService(intent);
    }/*from  ww w  . j av  a 2 s.com*/
}

From source file:eu.hydrologis.geopaparazzi.maptools.FeaturePagerActivity.java

public void onGotoFeature(View view) {
    try {/*from  ww  w  .j a v  a2  s  . c  om*/
        Geometry geometry = FeatureUtilities.getGeometry(selectedFeature);
        Coordinate centroid = geometry.getCentroid().getCoordinate();
        Context context = view.getContext();
        Intent intent = new Intent(context, MapsSupportService.class);
        intent.putExtra(MapsSupportService.CENTER_ON_POSITION_REQUEST, true);
        intent.putExtra(LibraryConstants.LONGITUDE, centroid.x);
        intent.putExtra(LibraryConstants.LATITUDE, centroid.y);
        context.startService(intent);

        ToolGroup activeToolGroup = EditManager.INSTANCE.getActiveToolGroup();
        if (activeToolGroup instanceof OnSelectionToolGroup) {
            OnSelectionToolGroup toolGroup = (OnSelectionToolGroup) activeToolGroup;
            toolGroup.setSelectedFeatures(Arrays.asList(selectedFeature));
        }

        finish();
    } catch (java.lang.Exception e) {
        GPLog.error(this, null, e);
    }

}

From source file:com.android.mail.utils.NotificationActionUtils.java

/**
 * Broadcasts an {@link Intent} to inform the app to resend its notifications.
 */// w  ww.  j av  a  2s .  c o  m
public static void resendNotifications(final Context context, final Account account, final Folder folder) {
    LogUtils.i(LOG_TAG, "resendNotifications account: %s, folder: %s",
            account == null ? null : LogUtils.sanitizeName(LOG_TAG, account.getEmailAddress()),
            folder == null ? null : LogUtils.sanitizeName(LOG_TAG, folder.name));

    final Intent intent = new Intent(MailIntentService.ACTION_RESEND_NOTIFICATIONS);
    intent.setPackage(context.getPackageName()); // Make sure we only deliver this to ourselves
    if (account != null) {
        intent.putExtra(Utils.EXTRA_ACCOUNT_URI, account.uri);
    }
    if (folder != null) {
        intent.putExtra(Utils.EXTRA_FOLDER_URI, folder.folderUri.fullUri);
    }
    context.startService(intent);
}

From source file:com.jetheis.android.makeitrain.billing.googleplay.GooglePlayBillingWrapper.java

private GooglePlayBillingWrapper(Context context, OnGooglePlayBillingReadyListener onReadyListener,
        OnGooglePlayVipModePurchaseFoundListener onPurchaseListener) {
    mContext = context;/*from   w  w  w . j a  v  a2 s . com*/
    mOnReadyListener = onReadyListener;
    mOnPurchaseListnener = onPurchaseListener;

    context.startService(new Intent(context, GooglePlayBillingService.class));

    mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBoundService = ((GooglePlayBillingService.GooglePlayBillingBinder) service).getService();
            mBoundService.checkIsBillingSupported(new OnGooglePlayBillingSupportResultListener() {

                @Override
                public void onGooglePlayBillingSupportResultFound(boolean billingSupported) {
                    if (billingSupported) {
                        Log.i(Constants.TAG, "Google Play billing ready");
                        if (mOnReadyListener != null) {
                            mOnReadyListener.onGooglePlayBillingReady();
                        }
                    } else {
                        Log.i(Constants.TAG, "Google Play billing is not supported");
                        if (mOnReadyListener != null) {
                            mOnReadyListener.onGooglePlayBillingNotSupported();
                        }
                    }
                }

            });

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBoundService = null;
        }

    };

    mContext.bindService(new Intent(context, GooglePlayBillingService.class), mConnection,
            Context.BIND_AUTO_CREATE);
}

From source file:org.y20k.transistor.PlayerService.java

public void startActionStop(Context context) {
    Log.v(LOG_TAG, "stopping playback service:");

    // stop player service using intent
    Intent intent = new Intent(context, PlayerService.class);
    intent.setAction(ACTION_STOP);/*from w w  w . j  ava2  s.c o m*/
    context.startService(intent);
}

From source file:com.cloudmine.api.db.RequestPerformerService.java

/**
 * This intercepts any uncaught exceptions and restarts the service
 *///from  www.ja v  a 2 s  . c om
private void catchUncaughtExceptions() {
    final Context applicationContext = getApplicationContext();
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {

            LOG.error("Crashed, restarting service ", throwable);
            applicationContext.startService(new Intent(applicationContext, RequestPerformerService.class));

            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    });
}

From source file:org.sufficientlysecure.keychain.service.PassphraseCacheService.java

/**
 * Gets a cached passphrase from memory by sending an intent to the service. This method is
 * designed to wait until the service returns the passphrase.
 *
 * @param context//from   www  .  j a  v  a2  s .c om
 * @param keyId
 * @return passphrase or null (if no passphrase is cached for this keyId)
 */
public static String getCachedPassphrase(Context context, long keyId) {
    Log.d(TAG, "getCachedPassphrase() get masterKeyId for " + keyId);

    Intent intent = new Intent(context, PassphraseCacheService.class);
    intent.setAction(ACTION_PASSPHRASE_CACHE_GET);

    final Object mutex = new Object();
    final Bundle returnBundle = new Bundle();

    HandlerThread handlerThread = new HandlerThread("getPassphraseThread");
    handlerThread.start();
    Handler returnHandler = new Handler(handlerThread.getLooper()) {
        @Override
        public void handleMessage(Message message) {
            if (message.obj != null) {
                String passphrase = ((Bundle) message.obj).getString(EXTRA_PASSPHRASE);
                returnBundle.putString(EXTRA_PASSPHRASE, passphrase);
            }
            synchronized (mutex) {
                mutex.notify();
            }
            // quit handlerThread
            getLooper().quit();
        }
    };

    // Create a new Messenger for the communication back
    Messenger messenger = new Messenger(returnHandler);
    intent.putExtra(EXTRA_KEY_ID, keyId);
    intent.putExtra(EXTRA_MESSENGER, messenger);
    // send intent to this service
    context.startService(intent);

    // Wait on mutex until passphrase is returned to handlerThread
    synchronized (mutex) {
        try {
            mutex.wait(3000);
        } catch (InterruptedException e) {
        }
    }

    if (returnBundle.containsKey(EXTRA_PASSPHRASE)) {
        return returnBundle.getString(EXTRA_PASSPHRASE);
    } else {
        return null;
    }
}