Example usage for android.app Notification FLAG_NO_CLEAR

List of usage examples for android.app Notification FLAG_NO_CLEAR

Introduction

In this page you can find the example usage for android.app Notification FLAG_NO_CLEAR.

Prototype

int FLAG_NO_CLEAR

To view the source code for android.app Notification FLAG_NO_CLEAR.

Click Source Link

Document

Bit to be bitwise-ored into the #flags field that should be set if the notification should not be canceled when the user clicks the Clear all button.

Usage

From source file:github.madmarty.madsonic.util.Util.java

public static void showPlayingNotification(final Context context, final DownloadServiceImpl downloadService,
        Handler handler, MusicDirectory.Entry song) {

    // Set the icon, scrolling text and timestamp
    final Notification notification = new Notification(R.drawable.stat_notify, song.getTitle(),
            System.currentTimeMillis());
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

    boolean playing = downloadService.getPlayerState() == PlayerState.STARTED;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        RemoteViews expandedContentView = new RemoteViews(context.getPackageName(),
                R.layout.notification_expanded);
        setupViews(expandedContentView, context, song, playing);
        notification.bigContentView = expandedContentView;
    }/*from   w  ww . j  av  a2 s  .co m*/

    RemoteViews smallContentView = new RemoteViews(context.getPackageName(), R.layout.notification);
    setupViews(smallContentView, context, song, playing);
    notification.contentView = smallContentView;

    Intent notificationIntent = new Intent(context, DownloadActivity.class);
    notification.contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    handler.post(new Runnable() {
        @Override
        public void run() {
            downloadService.startForeground(Constants.NOTIFICATION_ID_PLAYING, notification);
        }
    });

    // Update widget
    MadsonicWidgetProvider.notifyInstances(context, downloadService, true);
}

From source file:com.aniruddhc.acemusic.player.Services.AudioPlaybackService.java

/**
 * Builds and returns a fully constructed Notification for devices 
 * on Jelly Bean and above (API 16+).//  ww  w  .  j a v  a2 s.c o m
 */
@SuppressLint("NewApi")
private Notification buildJBNotification(SongHelper songHelper) {
    mNotificationBuilder = new NotificationCompat.Builder(mContext);
    mNotificationBuilder.setOngoing(true);
    mNotificationBuilder.setAutoCancel(false);
    mNotificationBuilder.setSmallIcon(R.drawable.notif_icon);

    //Open up the player screen when the user taps on the notification.
    Intent launchNowPlayingIntent = new Intent();
    launchNowPlayingIntent.setAction(AudioPlaybackService.LAUNCH_NOW_PLAYING_ACTION);
    PendingIntent launchNowPlayingPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(),
            0, launchNowPlayingIntent, 0);
    mNotificationBuilder.setContentIntent(launchNowPlayingPendingIntent);

    //Grab the notification layouts.
    RemoteViews notificationView = new RemoteViews(mContext.getPackageName(),
            R.layout.notification_custom_layout);
    RemoteViews expNotificationView = new RemoteViews(mContext.getPackageName(),
            R.layout.notification_custom_expanded_layout);

    //Initialize the notification layout buttons.
    Intent previousTrackIntent = new Intent();
    previousTrackIntent.setAction(AudioPlaybackService.PREVIOUS_ACTION);
    PendingIntent previousTrackPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            previousTrackIntent, 0);

    Intent playPauseTrackIntent = new Intent();
    playPauseTrackIntent.setAction(AudioPlaybackService.PLAY_PAUSE_ACTION);
    PendingIntent playPauseTrackPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            playPauseTrackIntent, 0);

    Intent nextTrackIntent = new Intent();
    nextTrackIntent.setAction(AudioPlaybackService.NEXT_ACTION);
    PendingIntent nextTrackPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            nextTrackIntent, 0);

    Intent stopServiceIntent = new Intent();
    stopServiceIntent.setAction(AudioPlaybackService.STOP_SERVICE);
    PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            stopServiceIntent, 0);

    //Check if audio is playing and set the appropriate play/pause button.
    if (mApp.getService().isPlayingMusic()) {
        notificationView.setImageViewResource(R.id.notification_base_play, R.drawable.btn_playback_pause_light);
        expNotificationView.setImageViewResource(R.id.notification_expanded_base_play,
                R.drawable.btn_playback_pause_light);
    } else {
        notificationView.setImageViewResource(R.id.notification_base_play, R.drawable.btn_playback_play_light);
        expNotificationView.setImageViewResource(R.id.notification_expanded_base_play,
                R.drawable.btn_playback_play_light);
    }

    //Set the notification content.
    expNotificationView.setTextViewText(R.id.notification_expanded_base_line_one, songHelper.getTitle());
    expNotificationView.setTextViewText(R.id.notification_expanded_base_line_two, songHelper.getArtist());
    expNotificationView.setTextViewText(R.id.notification_expanded_base_line_three, songHelper.getAlbum());

    notificationView.setTextViewText(R.id.notification_base_line_one, songHelper.getTitle());
    notificationView.setTextViewText(R.id.notification_base_line_two, songHelper.getArtist());

    //Set the states of the next/previous buttons and their pending intents.
    if (mApp.getService().isOnlySongInQueue()) {
        //This is the only song in the queue, so disable the previous/next buttons.
        expNotificationView.setViewVisibility(R.id.notification_expanded_base_next, View.INVISIBLE);
        expNotificationView.setViewVisibility(R.id.notification_expanded_base_previous, View.INVISIBLE);
        expNotificationView.setOnClickPendingIntent(R.id.notification_expanded_base_play,
                playPauseTrackPendingIntent);

        notificationView.setViewVisibility(R.id.notification_base_next, View.INVISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_previous, View.INVISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);

    } else if (mApp.getService().isFirstSongInQueue()) {
        //This is the the first song in the queue, so disable the previous button.
        expNotificationView.setViewVisibility(R.id.notification_expanded_base_previous, View.INVISIBLE);
        expNotificationView.setViewVisibility(R.id.notification_expanded_base_next, View.VISIBLE);
        expNotificationView.setOnClickPendingIntent(R.id.notification_expanded_base_play,
                playPauseTrackPendingIntent);
        expNotificationView.setOnClickPendingIntent(R.id.notification_expanded_base_next,
                nextTrackPendingIntent);

        notificationView.setViewVisibility(R.id.notification_base_previous, View.INVISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_next, View.VISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);

    } else if (mApp.getService().isLastSongInQueue()) {
        //This is the last song in the cursor, so disable the next button.
        expNotificationView.setViewVisibility(R.id.notification_expanded_base_previous, View.VISIBLE);
        expNotificationView.setViewVisibility(R.id.notification_expanded_base_next, View.INVISIBLE);
        expNotificationView.setOnClickPendingIntent(R.id.notification_expanded_base_play,
                playPauseTrackPendingIntent);
        expNotificationView.setOnClickPendingIntent(R.id.notification_expanded_base_next,
                nextTrackPendingIntent);

        notificationView.setViewVisibility(R.id.notification_base_previous, View.VISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_next, View.INVISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);

    } else {
        //We're smack dab in the middle of the queue, so keep the previous and next buttons enabled.
        expNotificationView.setViewVisibility(R.id.notification_expanded_base_previous, View.VISIBLE);
        expNotificationView.setViewVisibility(R.id.notification_expanded_base_next, View.VISIBLE);
        expNotificationView.setOnClickPendingIntent(R.id.notification_expanded_base_play,
                playPauseTrackPendingIntent);
        expNotificationView.setOnClickPendingIntent(R.id.notification_expanded_base_next,
                nextTrackPendingIntent);
        expNotificationView.setOnClickPendingIntent(R.id.notification_expanded_base_previous,
                previousTrackPendingIntent);

        notificationView.setViewVisibility(R.id.notification_base_previous, View.VISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_next, View.VISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);

    }

    //Set the "Stop Service" pending intents.
    expNotificationView.setOnClickPendingIntent(R.id.notification_expanded_base_collapse,
            stopServicePendingIntent);
    notificationView.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);

    //Set the album art.
    expNotificationView.setImageViewBitmap(R.id.notification_expanded_base_image, songHelper.getAlbumArt());
    notificationView.setImageViewBitmap(R.id.notification_base_image, songHelper.getAlbumArt());

    //Attach the shrunken layout to the notification.
    mNotificationBuilder.setContent(notificationView);

    //Build the notification object.
    Notification notification = mNotificationBuilder.build();

    //Attach the expanded layout to the notification and set its flags.
    notification.bigContentView = expNotificationView;
    notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR
            | Notification.FLAG_ONGOING_EVENT;

    return notification;
}

From source file:ro.ciubex.keepscreenlock.MainApplication.java

/**
 * Build the notification item.// w w  w  .j a  va 2 s .co  m
 *
 * @param allowDismiss Flag used to create a dismissible notification.
 * @return The notification item.
 */
private Notification getNotification(boolean allowDismiss) {
    if (mNotification == null) {
        PendingIntent pendingScreenLockIntent = PendingIntent.getBroadcast(this, 0,
                getNotificationScreenLockIntent(), PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
        remoteViews.setOnClickPendingIntent(R.id.lockScreenView, pendingScreenLockIntent);

        NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this);
        notifBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
        notifBuilder.setSmallIcon(R.mipmap.ic_launcher_red);
        notifBuilder.setContent(remoteViews);

        notifBuilder.setContentTitle(getString(R.string.app_name));
        notifBuilder.setContentText(getString(R.string.notification_text));

        mNotification = notifBuilder.build();
    }
    if (isNotificationAlwaysDismissible() || allowDismiss) {
        mNotification.flags &= ~Notification.FLAG_NO_CLEAR;
    } else {
        mNotification.flags |= Notification.FLAG_NO_CLEAR;
    }
    return mNotification;
}

From source file:com.adityarathi.muo.services.AudioPlaybackService.java

/**
 * Builds and returns a fully constructed Notification for devices 
 * on Ice Cream Sandwich (APIs 14 & 15).
 *//*from w w w  . j  av  a  2  s .  co m*/
private Notification buildICSNotification(SongHelper songHelper) {
    mNotificationBuilder = new NotificationCompat.Builder(mContext);
    mNotificationBuilder.setOngoing(true);
    mNotificationBuilder.setAutoCancel(false);
    mNotificationBuilder.setSmallIcon(R.mipmap.ic_launcher);

    //Open up the player screen when the user taps on the notification.
    Intent launchNowPlayingIntent = new Intent();
    launchNowPlayingIntent.setAction(AudioPlaybackService.LAUNCH_NOW_PLAYING_ACTION);
    PendingIntent launchNowPlayingPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(),
            0, launchNowPlayingIntent, 0);
    mNotificationBuilder.setContentIntent(launchNowPlayingPendingIntent);

    //Grab the notification layout.
    RemoteViews notificationView = new RemoteViews(mContext.getPackageName(),
            R.layout.notification_custom_layout);

    //Initialize the notification layout buttons.
    Intent previousTrackIntent = new Intent();
    previousTrackIntent.setAction(AudioPlaybackService.PREVIOUS_ACTION);
    PendingIntent previousTrackPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            previousTrackIntent, 0);

    Intent playPauseTrackIntent = new Intent();
    playPauseTrackIntent.setAction(AudioPlaybackService.PLAY_PAUSE_ACTION);
    PendingIntent playPauseTrackPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            playPauseTrackIntent, 0);

    Intent nextTrackIntent = new Intent();
    nextTrackIntent.setAction(AudioPlaybackService.NEXT_ACTION);
    PendingIntent nextTrackPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            nextTrackIntent, 0);

    Intent stopServiceIntent = new Intent();
    stopServiceIntent.setAction(AudioPlaybackService.STOP_SERVICE);
    PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            stopServiceIntent, 0);

    //Check if audio is playing and set the appropriate play/pause button.
    if (mApp.getService().isPlayingMusic()) {
        notificationView.setImageViewResource(R.id.notification_base_play, R.mipmap.ic_launcher);
    } else {
        notificationView.setImageViewResource(R.id.notification_base_play, R.mipmap.ic_launcher);
    }

    //Set the notification content.    
    notificationView.setTextViewText(R.id.notification_base_line_one, songHelper.getTitle());
    notificationView.setTextViewText(R.id.notification_base_line_two, songHelper.getArtist());

    //Set the states of the next/previous buttons and their pending intents.
    if (mApp.getService().isOnlySongInQueue()) {
        //This is the only song in the queue, so disable the previous/next buttons.
        notificationView.setViewVisibility(R.id.notification_base_next, View.INVISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_previous, View.INVISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);

    } else if (mApp.getService().isFirstSongInQueue()) {
        //This is the the first song in the queue, so disable the previous button. 
        notificationView.setViewVisibility(R.id.notification_base_previous, View.INVISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_next, View.VISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);

    } else if (mApp.getService().isLastSongInQueue()) {
        //This is the last song in the cursor, so disable the next button.    
        notificationView.setViewVisibility(R.id.notification_base_previous, View.VISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_next, View.INVISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);

    } else {
        //We're smack dab in the middle of the queue, so keep the previous and next buttons enabled.       
        notificationView.setViewVisibility(R.id.notification_base_previous, View.VISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_next, View.VISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);

    }

    //Set the "Stop Service" pending intent.
    notificationView.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);

    //Set the album art.
    notificationView.setImageViewBitmap(R.id.notification_base_image, songHelper.getAlbumArt());

    //Attach the shrunken layout to the notification.
    mNotificationBuilder.setContent(notificationView);

    //Build the notification object and set its flags.
    Notification notification = mNotificationBuilder.build();
    notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR
            | Notification.FLAG_ONGOING_EVENT;

    return notification;
}

From source file:com.aniruddhc.acemusic.player.Services.AudioPlaybackService.java

/**
 * Builds and returns a fully constructed Notification for devices 
 * on Ice Cream Sandwich (APIs 14 & 15).
 *///ww w. ja v  a 2s  .c  o m
private Notification buildICSNotification(SongHelper songHelper) {
    mNotificationBuilder = new NotificationCompat.Builder(mContext);
    mNotificationBuilder.setOngoing(true);
    mNotificationBuilder.setAutoCancel(false);
    mNotificationBuilder.setSmallIcon(R.drawable.notif_icon);

    //Open up the player screen when the user taps on the notification.
    Intent launchNowPlayingIntent = new Intent();
    launchNowPlayingIntent.setAction(AudioPlaybackService.LAUNCH_NOW_PLAYING_ACTION);
    PendingIntent launchNowPlayingPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(),
            0, launchNowPlayingIntent, 0);
    mNotificationBuilder.setContentIntent(launchNowPlayingPendingIntent);

    //Grab the notification layout.
    RemoteViews notificationView = new RemoteViews(mContext.getPackageName(),
            R.layout.notification_custom_layout);

    //Initialize the notification layout buttons.
    Intent previousTrackIntent = new Intent();
    previousTrackIntent.setAction(AudioPlaybackService.PREVIOUS_ACTION);
    PendingIntent previousTrackPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            previousTrackIntent, 0);

    Intent playPauseTrackIntent = new Intent();
    playPauseTrackIntent.setAction(AudioPlaybackService.PLAY_PAUSE_ACTION);
    PendingIntent playPauseTrackPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            playPauseTrackIntent, 0);

    Intent nextTrackIntent = new Intent();
    nextTrackIntent.setAction(AudioPlaybackService.NEXT_ACTION);
    PendingIntent nextTrackPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            nextTrackIntent, 0);

    Intent stopServiceIntent = new Intent();
    stopServiceIntent.setAction(AudioPlaybackService.STOP_SERVICE);
    PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
            stopServiceIntent, 0);

    //Check if audio is playing and set the appropriate play/pause button.
    if (mApp.getService().isPlayingMusic()) {
        notificationView.setImageViewResource(R.id.notification_base_play, R.drawable.btn_playback_pause_light);
    } else {
        notificationView.setImageViewResource(R.id.notification_base_play, R.drawable.btn_playback_play_light);
    }

    //Set the notification content.    
    notificationView.setTextViewText(R.id.notification_base_line_one, songHelper.getTitle());
    notificationView.setTextViewText(R.id.notification_base_line_two, songHelper.getArtist());

    //Set the states of the next/previous buttons and their pending intents.
    if (mApp.getService().isOnlySongInQueue()) {
        //This is the only song in the queue, so disable the previous/next buttons.
        notificationView.setViewVisibility(R.id.notification_base_next, View.INVISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_previous, View.INVISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);

    } else if (mApp.getService().isFirstSongInQueue()) {
        //This is the the first song in the queue, so disable the previous button. 
        notificationView.setViewVisibility(R.id.notification_base_previous, View.INVISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_next, View.VISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);

    } else if (mApp.getService().isLastSongInQueue()) {
        //This is the last song in the cursor, so disable the next button.    
        notificationView.setViewVisibility(R.id.notification_base_previous, View.VISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_next, View.INVISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);

    } else {
        //We're smack dab in the middle of the queue, so keep the previous and next buttons enabled.       
        notificationView.setViewVisibility(R.id.notification_base_previous, View.VISIBLE);
        notificationView.setViewVisibility(R.id.notification_base_next, View.VISIBLE);
        notificationView.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
        notificationView.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);

    }

    //Set the "Stop Service" pending intent.
    notificationView.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);

    //Set the album art.
    notificationView.setImageViewBitmap(R.id.notification_base_image, songHelper.getAlbumArt());

    //Attach the shrunken layout to the notification.
    mNotificationBuilder.setContent(notificationView);

    //Build the notification object and set its flags.
    Notification notification = mNotificationBuilder.build();
    notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR
            | Notification.FLAG_ONGOING_EVENT;

    return notification;
}

From source file:nu.yona.app.ui.YonaActivity.java

private void showDeviceRestartNotification(final String message) {
    Intent intent = new Intent(Intent.ACTION_REBOOT);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    Notification notification = new Notification.Builder(this).setContentTitle(getString(R.string.appname))
            .setContentText(message).setTicker(getString(R.string.appname)).setWhen(0)
            .setVibrate(new long[] { 1, 1, 1 }).setDefaults(Notification.DEFAULT_SOUND)
            .setStyle(new Notification.BigTextStyle().bigText(message)).setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setContentIntent(pIntent).setAutoCancel(false).build();

    notification.flags |= Notification.FLAG_NO_CLEAR;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
}