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:com.android.messaging.ui.UIIntentsImpl.java

@Override
public void kickMediaScanner(final Context context, final String volume) {
    final Intent intent = new Intent(MEDIA_SCANNER_SCAN_ACTION)
            .putExtra(MediaStore.MEDIA_SCANNER_VOLUME, volume)
            .setClassName(MEDIA_SCANNER_PACKAGE, MEDIA_SCANNER_CLASS);
    context.startService(intent);
}

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

/**
 * Broadcasts an {@link Intent} to inform the app to resend its notifications.
 *//* www. j a va2s. c  o m*/
public static void resendNotifications(final Context context, final Account account, final Folder folder) {
    // Fix NPE when parameter is null in NotificationActionUtils
    LogUtils.i(LOG_TAG, "resendNotifications account: %s, folder: %s",
            LogUtils.sanitizeName(LOG_TAG, account == null ? "null" : account.getEmailAddress()),
            LogUtils.sanitizeName(LOG_TAG, folder == null ? "null" : folder.name));

    //M upgrade: Bug: 17713589 Gmail crashes when accessing an old notification for an account that has been deleted.
    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:org.catrobat.catroid.utils.DownloadUtil.java

public void startMediaDownload(Context context, String url, String mediaName, String filePath) {
    Intent downloadIntent = new Intent(context, MediaDownloadService.class);
    downloadIntent.putExtra(MediaDownloadService.RECEIVER_TAG, new DownloadMediaReceiver(new Handler()));
    downloadIntent.putExtra(MediaDownloadService.URL_TAG, url);
    downloadIntent.putExtra(MediaDownloadService.MEDIA_FILE_PATH, filePath);
    webViewActivity.createProgressDialog(mediaName);
    webViewActivity.setResultIntent(/* w w  w  . j  a v  a  2 s  .c om*/
            webViewActivity.getResultIntent().putExtra(WebViewActivity.MEDIA_FILE_PATH, filePath));
    context.startService(downloadIntent);
}

From source file:com.marianhello.bgloc.BootCompletedReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "Received boot completed");
    ConfigurationDAO dao = DAOFactory.createConfigurationDAO(context);
    Config config = null;// w  w  w.j ava  2 s  .  c  o m

    try {
        config = dao.retrieveConfiguration();
    } catch (JSONException e) {
        //noop
    }

    if (config == null) {
        return;
    }

    Log.d(TAG, "Boot completed " + config.toString());

    if (config.getStartOnBoot()) {
        Log.i(TAG, "Starting service after boot");
        Intent locationServiceIntent = new Intent(context, LocationService.class);
        locationServiceIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
        locationServiceIntent.putExtra("config", config);

        context.startService(locationServiceIntent);
    }
}

From source file:com.mattprecious.prioritysms.receiver.AlarmReceiver.java

private void doAlarm(Context context, BaseProfile profile, String number, String message) {
    // Maintain a cpu wake lock until the AlarmActivity and AlarmService can
    // pick it up.
    AlarmAlertWakeLock.acquireCpuWakeLock(context);

    /* Close dialogs and window shade */
    Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    context.sendBroadcast(closeDialogs);

    // Play the alarm alert and vibrate the device.
    Intent playAlarm = new Intent(Intents.ACTION_ALERT);
    playAlarm.putExtra(Intents.EXTRA_PROFILE, profile);
    context.startService(playAlarm);

    Intent dismissIntent = new Intent(Intents.ACTION_DISMISS);
    dismissIntent.putExtra(Intents.EXTRA_PROFILE, profile);
    PendingIntent pendingDismiss = PendingIntent.getBroadcast(context, profile.getId(), dismissIntent, 0);

    Intent replyIntent = new Intent(Intents.ACTION_REPLY);
    replyIntent.putExtra(Intents.EXTRA_PROFILE, profile);
    PendingIntent pendingReply = PendingIntent.getBroadcast(context, profile.getId(), replyIntent, 0);

    Intent callIntent = new Intent(Intents.ACTION_CALL);
    callIntent.putExtra(Intents.EXTRA_PROFILE, profile);
    PendingIntent pendingCall = PendingIntent.getBroadcast(context, profile.getId(), callIntent, 0);

    Intent activityIntent = new Intent(context, AlarmActivity.class);
    activityIntent.setAction(String.valueOf(System.currentTimeMillis()));
    activityIntent.putExtra(Intents.EXTRA_PROFILE, profile);
    activityIntent.putExtra(Intents.EXTRA_NUMBER, number);
    activityIntent.putExtra(Intents.EXTRA_MESSAGE, message);
    activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    PendingIntent pendingActivity = PendingIntent.getActivity(context, profile.getId(), activityIntent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentTitle(profile.getName()).setSmallIcon(R.drawable.ic_stat_alarm).setAutoCancel(false)
            .setPriority(NotificationCompat.PRIORITY_MAX).setDefaults(Notification.DEFAULT_LIGHTS)
            .addAction(android.R.drawable.ic_menu_close_clear_cancel,
                    context.getString(R.string.notif_action_dismiss), pendingDismiss)
            .addAction(android.R.drawable.ic_menu_send, context.getString(R.string.notif_action_reply),
                    pendingReply)/*from w ww .  j a v a 2 s.c  om*/
            .addAction(android.R.drawable.ic_menu_call, context.getString(R.string.notif_action_call),
                    pendingCall)
            .setFullScreenIntent(pendingActivity, true).setContentIntent(pendingActivity)
            .setDeleteIntent(pendingDismiss);

    Notification notif = builder.build();

    // Send the notification using the alarm id to easily identify the
    // correct notification.
    NotificationManager nm = getNotificationManager(context);
    nm.notify(profile.getId(), notif);

    // full screen intent doesn't do anything pre-honeycomb
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        context.startActivity(activityIntent);
    }
}

From source file:de.ub0r.android.lib.DonationHelper.java

/**
 * Check if ads should be hidden.//from   w  ww .  j a  v  a2 s.co m
 * 
 * @param context
 *            {@link Context}
 * @return true if ads should be hidden
 */
public static boolean hideAds(final Context context) {
    PackageManager pm = context.getPackageManager();
    Intent donationCheck = new Intent(DONATOR_BROADCAST_CHECK);
    ResolveInfo ri = pm.resolveService(donationCheck, 0);
    // Log.d(TAG, "ri: " + ri);
    int match = PackageManager.SIGNATURE_UNKNOWN_PACKAGE;
    if (ri != null) {
        Log.d(TAG, "found package: " + ri.serviceInfo.packageName);
        ComponentName cn = new ComponentName(ri.serviceInfo.packageName, ri.serviceInfo.name);
        // Log.d(TAG, "component name: " + cn);
        int i = pm.getComponentEnabledSetting(cn);
        // Log.d(TAG, "component status: " + i);
        // Log.d(TAG, "package status: " + ri.serviceInfo.enabled);
        if (i == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                || i == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT && ri.serviceInfo.enabled) {
            match = pm.checkSignatures(context.getPackageName(), ri.serviceInfo.packageName);
        } else {
            Log.w(TAG, ri.serviceInfo.packageName + ": " + ri.serviceInfo.enabled);
        }
    }

    Log.i(TAG, "signature match: " + match);
    if (match != PackageManager.SIGNATURE_UNKNOWN_PACKAGE) {
        if (Math.random() < CHECK_DONATOR_LIC) {
            // verify donator license
            ComponentName cn = context.startService(donationCheck);
            Log.d(TAG, "Started service: " + cn);
            if (cn == null) {
                return false;
            }
        }
        return match == PackageManager.SIGNATURE_MATCH;
    }
    pm = null;

    // no donator app installed, check donation traditionally
    // do not drop legacy donators
    boolean ret = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREFS_HIDEADS, false);
    Log.d(TAG, "legacy donation check: " + ret);
    return ret;
}

From source file:hku.fyp14017.blencode.utils.DownloadUtil.java

public void startDownload(Context context, String url, String programName) {
    programDownloadQueue.add(programName.toLowerCase(Locale.getDefault()));
    Intent downloadIntent = new Intent(context, ProjectDownloadService.class);
    downloadIntent.putExtra(ProjectDownloadService.RECEIVER_TAG, new DownloadReceiver(new Handler()));
    downloadIntent.putExtra(ProjectDownloadService.DOWNLOAD_NAME_TAG, programName);
    downloadIntent.putExtra(ProjectDownloadService.URL_TAG, url);
    StatusBarNotificationManager manager = StatusBarNotificationManager.getInstance();
    int notificationId = manager.createDownloadNotification(context, programName);
    downloadIntent.putExtra(ProjectDownloadService.ID_TAG, notificationId);
    context.startService(downloadIntent);
}

From source file:it.jaschke.alexandria.model.view.BookDetailViewModel.java

/**
 * Requests {@link BookService} to delete the book for which the detail data
 * is shown and publishes a {@link BookDeletionEvent} on the {@link EventBus}.
 * If no book is set, does nothing.//ww w  . ja va 2 s. co m
 *
 * @param context the {@link Context} used to comunicate with the
 *     {@link BookService} to request the book's deletion.
 */
public void deleteBook(Context context) {
    if (mBook == null) {
        Log.i(LOG_TAG, "Ignoring deletion request. No book set.");
        return;
    }
    Intent bookIntent = new Intent(context, BookService.class);
    bookIntent.putExtra(BookService.EXTRA_BOOK, Parcels.wrap(mBook));
    bookIntent.setAction(BookService.ACTION_DELETE_BOOK);
    context.startService(bookIntent);
    EventBus.getDefault().post(new BookDeletionEvent(mBook));
}

From source file:com.warp10.app.LocationService.java

/**
 * Command to launch the location service
 * @param context application context//ww  w . j a v  a 2 s  .  c o  m
 * @param isListenGPS iif user want to listen to GPS
 * @param isListenNetwork iif user want to listen to Network location
 */
protected void handleStartCommand(Context context, boolean isListenGPS, boolean isListenNetwork,
        boolean recordGPS, boolean recordNetwork, String prefix) {
    Intent intentServiceGeo = new Intent(context, LocationService.class);
    intentServiceGeo.setAction(ACTION_START);
    intentServiceGeo.putExtra(EXTRA_LISTGPS, isListenGPS);
    intentServiceGeo.putExtra(EXTRA_LISTNET, isListenNetwork);
    intentServiceGeo.putExtra(EXTRA_RECGPS, recordGPS);
    intentServiceGeo.putExtra(EXTRA_RECNET, recordNetwork);
    intentServiceGeo.putExtra(EXTRA_PREFIX, prefix);
    context.startService(intentServiceGeo);
}

From source file:com.android.contacts.ContactSaveService.java

/**
 * Utility method that starts service and handles exception.
 */// w w  w.  j av a  2s . c  om
public static void startService(Context context, Intent intent) {
    try {
        context.startService(intent);
    } catch (Exception exception) {
        Toast.makeText(context, R.string.contactGenericErrorToast, Toast.LENGTH_SHORT).show();
    }
}