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:com.umundus.service.NCallServiceOld.java

public void onCreate() {
    Log.d(TAG, "[*]Service-onCreate");
    super.onCreate();

    IntentFilter filter = new IntentFilter(ACTION_START_PATTERN_SEND_DATA);
    filter.addAction(ACTION_END_PATTERN_SEND_DATA);
    registerReceiver(mBroadcastReceiver, filter);

    int myID = 1234;
    Intent intent = new Intent(this, NCallServiceOld.class);
    //PendingIntent pendIntent = PendingIntent.getActivity(this, 0, bindIntent, 0);
    PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_NO_CREATE);
    Notification notice = new Notification.Builder(this).setContentTitle("(Rang)")
            .setContentText("(Rang)? .").setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)).setOngoing(true)
            .setContentIntent(pi).build();
    notice.flags |= Notification.FLAG_NO_CLEAR;
    notice.flags |= Notification.FLAG_ONGOING_EVENT;
    startForeground(myID, notice);/*from   w w w  . ja v  a2s. com*/

    AlarmManager al = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    al.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000 * 15, pi);
}

From source file:im.neon.services.EventStreamService.java

/**
 * @return the polling thread listener notification
 *///  ww w.j  a va 2  s .  c  o  m
@SuppressLint("NewApi")
private Notification buildForegroundServiceNotification() {
    // build the pending intent go to the home screen if this is clicked.
    Intent i = new Intent(this, VectorHomeActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

    // build the notification builder
    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this);
    notifBuilder.setSmallIcon(R.drawable.permanent_notification_transparent);
    notifBuilder.setWhen(System.currentTimeMillis());
    notifBuilder.setContentTitle(getString(R.string.app_name));
    notifBuilder.setContentText(NOTIFICATION_SUB_TITLE);
    notifBuilder.setContentIntent(pi);

    // hide the notification from the status bar
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notifBuilder.setPriority(NotificationCompat.PRIORITY_MIN);
    }

    Notification notification = notifBuilder.build();
    notification.flags |= Notification.FLAG_NO_CLEAR;

    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // some devices crash if this field is not set
        // even if it is deprecated

        // setLatestEventInfo() is deprecated on Android M, so we try to use
        // reflection at runtime, to avoid compiler error: "Cannot resolve method.."
        try {
            Method deprecatedMethod = notification.getClass().getMethod("setLatestEventInfo", Context.class,
                    CharSequence.class, CharSequence.class, PendingIntent.class);
            deprecatedMethod.invoke(notification, this, getString(R.string.app_name), NOTIFICATION_SUB_TITLE,
                    pi);
        } catch (Exception ex) {
            Log.e(LOG_TAG, "## buildNotification(): Exception - setLatestEventInfo() Msg=" + ex.getMessage());
        }
    }

    return notification;
}

From source file:de.mangelow.throughput.NotificationService.java

@SuppressWarnings("deprecation")
private void modifyNotification(int drawable, String ticker, String title, String subtitle, Intent i) {

    boolean showticker = MainActivity.loadBooleanPref(context, MainActivity.SHOWTICKER,
            MainActivity.SHOWTICKER_DEFAULT);
    if (!showticker)
        ticker = null;/*from ww w .  j  av a  2s  . c o  m*/

    NotificationManager nmanager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent pi = PendingIntent.getActivity(this, 0, i, NOTIFICATION_ID);
    Notification n = null;

    if (Build.VERSION.SDK_INT < 11) {

        n = new Notification(drawable, ticker, System.currentTimeMillis());
        n.flags |= Notification.FLAG_NO_CLEAR;
        n.setLatestEventInfo(this, title, subtitle, pi);

    } else {

        if (nb == null) {
            nb = new Notification.Builder(context);
            nb.setPriority(Notification.PRIORITY_LOW);
            nb.setAutoCancel(true);
        }

        nb.setSmallIcon(drawable);
        if (ticker != null)
            nb.setTicker(ticker);
        nb.setContentTitle(title);
        nb.setContentText(subtitle);
        nb.setContentIntent(pi);

        n = nb.build();
        n.flags = Notification.FLAG_NO_CLEAR;

    }

    nmanager.notify(NOTIFICATION_ID, n);

    //

    if (mResultReceiver != null) {

        Bundle bundle = new Bundle();
        bundle.putInt("drawable", drawable);
        bundle.putString("title", title);
        bundle.putString("subtitle", subtitle);
        mResultReceiver.send(0, bundle);

    }

}

From source file:gov.nasa.arc.geocam.geocam.GeoCamService.java

private void buildNotification(CharSequence title, CharSequence notifyText) {
    Intent notificationIntent = new Intent(getApplication(), GeoCamMobile.class);
    //notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    if (mNotification == null) {
        mNotification = new Notification(R.drawable.camera_48x48, notifyText, System.currentTimeMillis());
        mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
        mNotification.flags |= Notification.FLAG_NO_CLEAR;
    }/*from  ww w . ja  v  a2  s  .co m*/
    mNotification.setLatestEventInfo(getApplicationContext(), title, notifyText, contentIntent);
}

From source file:com.nd.android.u.square.service.MusicPlaybackService.java

Notification buildNotification(String text) {
    //        Log.d("yulin", "buildNotification, musicName = " + musicName + ", mIsPause = " + mIsPause);

    NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this);
    mNotificationBuilder.setOngoing(true);
    mNotificationBuilder.setAutoCancel(false);
    mNotificationBuilder.setSmallIcon(R.drawable.ic_square_notification_music_play);
    mNotificationBuilder.setTicker(text);

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

    Intent stopServiceIntent = new Intent(ACTION_STOP);
    PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
            stopServiceIntent, 0);//ww w.jav  a2  s  .c o m

    //Set the notification content.

    notificationView.setTextViewText(R.id.notification_base_line_one, text);

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

    //Set the album art.
    notificationView.setImageViewResource(R.id.notification_base_image, R.drawable.ic_square_music_default);

    //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.orange.oidc.tim.service.Service.java

private void showNotification(boolean bProtect) {
    Logd(TAG, "show protected icon " + bProtect);

    // this is it, we'll build the notification!
    // in the addAction method, if you don't want any icon, just set the first param to 0
    Notification mNotification = null;

    if (bProtect) {
        mNotification = new Notification.Builder(this)

                .setContentTitle("TIM").setContentText("privacy protected").setSmallIcon(R.drawable.masked_on)
                .setAutoCancel(false).build();
    } else {/*ww w .  j  a v  a2  s. c o m*/
        mNotification = new Notification.Builder(this)

                .setContentTitle("TIM").setContentText("privacy not protected")
                .setSmallIcon(R.drawable.masked_off).setAutoCancel(false).build();
    }

    // to make it non clearable
    mNotification.flags |= Notification.FLAG_NO_CLEAR;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // If you want to hide the notification after it was selected, do the code below
    // myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, mNotification);
}

From source file:github.daneren2005.dsub.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_playing, 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  w  w .java  2  s.  com*/

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

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    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
    DSubWidgetProvider.notifyInstances(context, downloadService, true);
}

From source file:de.tudresden.inf.rn.mobilis.mxa.XMPPRemoteService.java

/**
 * @author Tobias Rho// w  ww  .j ava 2  s . com
 */
private void showConnectionNotification() {
    if (RUN_IN_FOREGROUND) {
        Notification note = new NotificationCompat.Builder(this)
                .setContentTitle(getString(R.string.sb_txt_title))
                .setContentText(getString(R.string.sb_txt_text)).setSmallIcon(R.drawable.stat_notify_chat)
                .build();

        Intent i = new Intent(this, MainActivity.class);

        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

        note.setLatestEventInfo(this, getString(R.string.sb_txt_title), getString(R.string.sb_txt_text), pi);
        note.flags |= Notification.FLAG_NO_CLEAR;

        startForeground(NOTIFICATION_ID, note);
    } else {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Notification status = new Notification(R.drawable.stat_notify_chat, getString(R.string.sb_txt_text),
                System.currentTimeMillis());
        status.setLatestEventInfo(XMPPRemoteService.this, getString(R.string.sb_txt_title),
                getString(R.string.sb_txt_text), PendingIntent.getActivity(XMPPRemoteService.this, 0,
                        new Intent(ConstMXA.INTENT_SERVICEMONITOR), 0));
        status.flags |= Notification.FLAG_ONGOING_EVENT;
        status.icon = R.drawable.stat_notify_chat;
        nm.notify(XMPPSERVICE_STATUS, status);
    }
}

From source file:co.beem.project.beem.FbTextService.java

public void startForeGroundWithnotification(int statusStringId) {

    //       Notification note=new Notification(R.drawable.ic_stat_pending_notification,
    //                getString(R.string.notification_welcome),
    //                System.currentTimeMillis());
    isStartingForeground = true;//w  ww  . j ava2  s  .c o  m
    Intent i = new Intent(this, FbTextMainActivity.class);

    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

    if (statusStringId == 0) {
        int statusState = mSettings.getInt(FbTextApplication.STATUS_KEY, 0);
        statusStringId = statusState == 0 ? R.string.notification_online : R.string.notification_invisible;
    }

    Notification note = new NotificationCompat.Builder(this).setContentTitle(getString(statusStringId))
            .setContentText(getString(R.string.notification_description))
            .setSmallIcon(R.drawable.ic_stat_pending_notification).setContentIntent(pi).build();

    /*note.setLatestEventInfo(this, getString(statusStringId),
        getString(R.string.notification_description),
        pi);*/
    note.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(1080, note);
}

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

/**
 * Builds and returns a fully constructed Notification for devices 
 * on Jelly Bean and above (API 16+).//from w w  w . ja  v  a  2  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.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 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.ic_play);
        expNotificationView.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_play);
    } else {
        notificationView.setImageViewResource(R.id.notification_base_play, R.drawable.ic_play);
        expNotificationView.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_play);
    }

    //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;
}