Example usage for android.app Service startForeground

List of usage examples for android.app Service startForeground

Introduction

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

Prototype

public final void startForeground(int id, Notification notification) 

Source Link

Document

If your service is started (running through Context#startService(Intent) ), then also make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state.

Usage

From source file:com.schoentoon.connectbot.service.ConnectionNotifier.java

public void showRunningNotification(Service context) {
    context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context));
}

From source file:com.fastbootmobile.encore.service.ServiceNotification.java

/**
 * Posts or updates the notification as a foreground notification
 *
 * @param service The service instance//from ww w  .java  2s .  c  om
 */
public void notify(Service service) {
    service.startForeground(NOTIFICATION_ID, mNotification);
}

From source file:de.aw.awlib.AWNotification.java

public AWNotification startForegroundNotification(@NonNull Service service, @NonNull String contentText) {
    NotificationCompat.Builder mBuilder = getNotification();
    mBuilder.setContentTitle(contentTitle);
    mBuilder.setContentText(contentText);
    service.startForeground(mNotifyID, mBuilder.build());
    return this;
}

From source file:com.blackcrowsteam.musicstop.NotificationHelper.java

/**
 * Show notification//  w  w w .  ja v a 2s  .  c om
 * @param s Service
 * @param title title 
 * @param message message
 */
public void setMessage(Service s, CharSequence title, CharSequence message) {

    Context context = s.getApplicationContext();

    Intent notificationIntent = new Intent(context, StopActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notif = new NotificationCompat.Builder(context).setContentTitle(title).setContentText(message)
            .setContentIntent(contentIntent).setSmallIcon(R.drawable.ic_launcher).setOngoing(true).setWhen(time)
            .build();

    // Pass the Notification to the NotificationManager:
    getManager(context).notify(id, notif);
    s.startForeground(id, notif);

}

From source file:com.ademsha.appnotifico.NotificationHelper.java

public static void notifyForForegroundService(final Service service,
        final PreparedNotification preparedNotification) {

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(preparedNotification.getContext())
            .setPriority(NotificationCompat.PRIORITY_MIN).setOngoing(true).setAutoCancel(false)
            .setOnlyAlertOnce(true);// w  ww  .  j  a v a 2  s .com

    if (preparedNotification.getPendingIntentForActivity() != null) {
        builder.setContentIntent(preparedNotification.getPendingIntentForService());
    }

    builder.setContentTitle(preparedNotification.getTitle());
    builder.setContentText(preparedNotification.getText());

    if (preparedNotification.getLargeIcon() != null) {
        builder.setLargeIcon(preparedNotification.getLargeIcon());
    }

    if (preparedNotification.getSmallIcon() > 0) {
        builder.setSmallIcon(preparedNotification.getSmallIcon());
    }

    builder.setTicker(preparedNotification.getTicker());

    if (!preparedNotification.getExpanded().equals("")
            && !preparedNotification.getExpandedSummary().equals("")) {
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(preparedNotification.getExpanded())
                .setBigContentTitle(preparedNotification.getTitle())
                .setSummaryText(preparedNotification.getExpandedSummary()));
    }

    if (preparedNotification.getPendingIntentForActivity() != null) {
        builder.setContentIntent(preparedNotification.getPendingIntentForActivity());
    }

    Notification notification;
    notification = builder.build();
    notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE
            | Notification.FLAG_NO_CLEAR;
    service.startForeground(10000, notification);
}

From source file:com.android.deskclock.alarms.AlarmNotifications.java

public static void showAlarmNotification(Service service, AlarmInstance instance) {
    LogUtils.v("Displaying alarm notification for alarm instance: " + instance.mId);

    Resources resources = service.getResources();
    NotificationCompat.Builder notification = new NotificationCompat.Builder(service)
            .setContentTitle(instance.getLabelOrDefault(service))
            .setContentText(AlarmUtils.getFormattedTime(service, instance.getAlarmTime()))
            .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false)
            .setDefaults(NotificationCompat.DEFAULT_LIGHTS).setWhen(0)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setLocalOnly(true);/*w  ww.  jav a 2s .  co m*/

    // Setup Snooze Action
    Intent snoozeIntent = AlarmStateManager.createStateChangeIntent(service, AlarmStateManager.ALARM_SNOOZE_TAG,
            instance, AlarmInstance.SNOOZE_STATE);
    snoozeIntent.putExtra(AlarmStateManager.FROM_NOTIFICATION_EXTRA, true);
    PendingIntent snoozePendingIntent = PendingIntent.getService(service, instance.hashCode(), snoozeIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(R.drawable.ic_snooze_24dp, resources.getString(R.string.alarm_alert_snooze_text),
            snoozePendingIntent);

    // Setup Dismiss Action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(service,
            AlarmStateManager.ALARM_DISMISS_TAG, instance, AlarmInstance.DISMISSED_STATE);
    dismissIntent.putExtra(AlarmStateManager.FROM_NOTIFICATION_EXTRA, true);
    PendingIntent dismissPendingIntent = PendingIntent.getService(service, instance.hashCode(), dismissIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(R.drawable.ic_alarm_off_24dp, resources.getString(R.string.alarm_alert_dismiss_text),
            dismissPendingIntent);

    // Setup Content Action
    Intent contentIntent = AlarmInstance.createIntent(service, AlarmActivity.class, instance.mId);
    notification.setContentIntent(PendingIntent.getActivity(service, instance.hashCode(), contentIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup fullscreen intent
    Intent fullScreenIntent = AlarmInstance.createIntent(service, AlarmActivity.class, instance.mId);
    // set action, so we can be different then content pending intent
    fullScreenIntent.setAction("fullscreen_activity");
    fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    notification.setFullScreenIntent(PendingIntent.getActivity(service, instance.hashCode(), fullScreenIntent,
            PendingIntent.FLAG_UPDATE_CURRENT), true);
    notification.setPriority(NotificationCompat.PRIORITY_MAX);

    clearNotification(service, instance);
    service.startForeground(instance.hashCode(), notification.build());
}

From source file:com.stasbar.knowyourself.alarms.AlarmNotifications.java

public static void showAlarmNotification(Service service, AlarmInstance instance) {
    LogUtils.v("Displaying alarm notification for alarm instance: " + instance.mId);

    Resources resources = service.getResources();
    NotificationCompat.Builder notification = new NotificationCompat.Builder(service)
            .setContentTitle(instance.getLabelOrDefault(service))
            .setContentText(AlarmUtils.getFormattedTime(service, instance.getAlarmTime()))
            .setColor(ContextCompat.getColor(service, R.color.default_background))
            .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false)
            .setDefaults(NotificationCompat.DEFAULT_LIGHTS).setWhen(0)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setLocalOnly(true);/*from   w ww  .  j  av  a2s  .  co  m*/

    // Setup Snooze Action
    Intent snoozeIntent = AlarmStateManager.createStateChangeIntent(service, AlarmStateManager.ALARM_SNOOZE_TAG,
            instance, AlarmInstance.SNOOZE_STATE);
    snoozeIntent.putExtra(AlarmStateManager.FROM_NOTIFICATION_EXTRA, true);
    PendingIntent snoozePendingIntent = PendingIntent.getService(service, instance.hashCode(), snoozeIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(R.drawable.ic_snooze_24dp, resources.getString(R.string.alarm_alert_snooze_text),
            snoozePendingIntent);

    // Setup Dismiss Action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(service,
            AlarmStateManager.ALARM_DISMISS_TAG, instance, AlarmInstance.DISMISSED_STATE);
    dismissIntent.putExtra(AlarmStateManager.FROM_NOTIFICATION_EXTRA, true);
    PendingIntent dismissPendingIntent = PendingIntent.getService(service, instance.hashCode(), dismissIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(R.drawable.ic_alarm_off_24dp, resources.getString(R.string.alarm_alert_dismiss_text),
            dismissPendingIntent);

    // Setup Content Action
    Intent contentIntent = AlarmInstance.createIntent(service, AlarmActivity.class, instance.mId);
    notification.setContentIntent(PendingIntent.getActivity(service, instance.hashCode(), contentIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup fullscreen intent
    Intent fullScreenIntent = AlarmInstance.createIntent(service, AlarmActivity.class, instance.mId);
    // set action, so we can be different then content pending intent
    fullScreenIntent.setAction("fullscreen_activity");
    fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    notification.setFullScreenIntent(PendingIntent.getActivity(service, instance.hashCode(), fullScreenIntent,
            PendingIntent.FLAG_UPDATE_CURRENT), true);
    notification.setPriority(NotificationCompat.PRIORITY_MAX);

    clearNotification(service, instance);
    service.startForeground(instance.hashCode(), notification.build());
}

From source file:com.androidinspain.deskclock.alarms.AlarmNotifications.java

static synchronized void showAlarmNotification(Service service, AlarmInstance instance) {
    LogUtils.v("Displaying alarm notification for alarm instance: " + instance.mId);

    Resources resources = service.getResources();
    NotificationCompat.Builder notification = new NotificationCompat.Builder(service)
            .setContentTitle(instance.getLabelOrDefault(service))
            .setContentText(AlarmUtils.getFormattedTime(service, instance.getAlarmTime()))
            .setColor(ContextCompat.getColor(service, com.androidinspain.deskclock.R.color.default_background))
            .setSmallIcon(com.androidinspain.deskclock.R.drawable.stat_notify_alarm).setOngoing(true)
            .setAutoCancel(false).setDefaults(NotificationCompat.DEFAULT_LIGHTS).setWhen(0)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setLocalOnly(true);/*from w  ww . ja  v a2 s  .  c  om*/

    // Setup Snooze Action
    Intent snoozeIntent = AlarmStateManager.createStateChangeIntent(service, AlarmStateManager.ALARM_SNOOZE_TAG,
            instance, AlarmInstance.SNOOZE_STATE);
    snoozeIntent.putExtra(AlarmStateManager.FROM_NOTIFICATION_EXTRA, true);
    PendingIntent snoozePendingIntent = PendingIntent.getService(service, ALARM_FIRING_NOTIFICATION_ID,
            snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(com.androidinspain.deskclock.R.drawable.ic_snooze_24dp,
            resources.getString(com.androidinspain.deskclock.R.string.alarm_alert_snooze_text),
            snoozePendingIntent);

    // Setup Dismiss Action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(service,
            AlarmStateManager.ALARM_DISMISS_TAG, instance, AlarmInstance.DISMISSED_STATE);
    dismissIntent.putExtra(AlarmStateManager.FROM_NOTIFICATION_EXTRA, true);
    PendingIntent dismissPendingIntent = PendingIntent.getService(service, ALARM_FIRING_NOTIFICATION_ID,
            dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(com.androidinspain.deskclock.R.drawable.ic_alarm_off_24dp,
            resources.getString(com.androidinspain.deskclock.R.string.alarm_alert_dismiss_text),
            dismissPendingIntent);

    // Setup Content Action
    Intent contentIntent = AlarmInstance.createIntent(service, AlarmActivity.class, instance.mId);
    notification.setContentIntent(PendingIntent.getActivity(service, ALARM_FIRING_NOTIFICATION_ID,
            contentIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup fullscreen intent
    Intent fullScreenIntent = AlarmInstance.createIntent(service, AlarmActivity.class, instance.mId);
    // set action, so we can be different then content pending intent
    fullScreenIntent.setAction("fullscreen_activity");
    fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    notification.setFullScreenIntent(PendingIntent.getActivity(service, ALARM_FIRING_NOTIFICATION_ID,
            fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT), true);
    notification.setPriority(NotificationCompat.PRIORITY_MAX);

    clearNotification(service, instance);
    service.startForeground(ALARM_FIRING_NOTIFICATION_ID, notification.build());
}