Example usage for android.app Notification FLAG_ONGOING_EVENT

List of usage examples for android.app Notification FLAG_ONGOING_EVENT

Introduction

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

Prototype

int FLAG_ONGOING_EVENT

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

Click Source Link

Document

Bit to be bitwise-ored into the #flags field that should be set if this notification is in reference to something that is ongoing, like a phone call.

Usage

From source file:com.wheelphone.remotemini.WheelphoneRemoteMini.java

public void onStart() {
    if (debugUsbComm) {
        logString = TAG + ": onStart";
        Log.d(TAG, logString);/*from w ww .  jav  a  2 s. c  om*/
        appendLog("debugUsbComm.txt", logString, false);
    }
    super.onStart();

    // Lock screen
    wl.acquire();

    Intent notificationIntent = new Intent(this, WheelphoneRemoteMini.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentIntent(pendingIntent).setWhen(System.currentTimeMillis())
            .setTicker(getText(R.string.notification_title))
            .setSmallIcon(R.drawable.wheelphone_logo_remote_mini_small)
            .setContentTitle(getText(R.string.notification_title))
            .setContentText(getText(R.string.notification_content));
    Notification notification = builder.getNotification();
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification);

}

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 w  w w . j a  va  2 s. c o 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);/* www .j  a v a2s  .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.f8full.casserolesencours.CasserolesEnCoursActivity.java

/**
 * On service click.//  w  w w .j a v  a  2  s.  co  m
 *
 * @param view the view
 */
public void onServiceClick(View view) {
    boolean isChecked = ((ToggleButton) view).isChecked();
    if (isChecked) {
        //turn on
        mAlohar.startServices();
        //StatusView.setText("Service is running!");
        ((TextView) findViewById(R.id.geolocationServiceStatus))
                .setText(getString(R.string.geolocationStatusOn));
        ((TextView) findViewById(R.id.geolocationServiceStatus))
                .setTextColor(getResources().getColor(R.color.text_red));
        ((TextView) findViewById(R.id.distanceFilterNA)).setVisibility(View.INVISIBLE);
        findViewById(R.id.FrequencyGroupLayout).setVisibility(View.VISIBLE);
        mDistanceFilterSpinner.setEnabled(true);

        //String freq = ((RadioButton)findViewById( ((RadioGroup)findViewById(R.id.pollFrequencyRadioGroup)).getCheckedRadioButtonId() )).getText().toString();
        String text = getString(R.string.geolocationStatusOnNotification);//String.format(getString(R.string.geolocationOnNotification), freq);
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = text;

        long when = System.currentTimeMillis();

        mNotification = new Notification(icon, tickerText, when);
        mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
        CharSequence contentText = getString(R.string.geolocationOnNotificationNoFreq);
        CharSequence contentTitle = getString(R.string.app_name);
        Intent notificationIntent = new Intent(this, CasserolesEnCoursActivity.class);
        mNotification.contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        mNotification.setLatestEventInfo(this, contentTitle, contentText, mNotification.contentIntent);
        mNotificationManager.notify(R.layout.main, mNotification);

    } else {
        //turn off
        mAlohar.stopServices();
        ((TextView) findViewById(R.id.geolocationServiceStatus))
                .setText(getString(R.string.geolocationStatusOff));
        ((TextView) findViewById(R.id.geolocationServiceStatus))
                .setTextColor(getResources().getColor(R.color.text_green));
        ((TextView) findViewById(R.id.distanceFilterNA)).setVisibility(View.VISIBLE);
        findViewById(R.id.FrequencyGroupLayout).setVisibility(View.INVISIBLE);
        mDistanceFilterSpinner.setSelection(0);
        mDistanceFilterSpinner.setEnabled(false);

        //((RadioGroup) findViewById(R.id.pollFrequencyRadioGroup)).check(-1);
        cancelActiveTasks();
        mLocationPollThreadExecutor.purge();

        //That is a hack, some code path must be missed, I'm tracking a thread leak bug when app is in background for a long time
        mLocationPollThreadExecutor.shutdown();
        mLocationPollThreadExecutor = new ScheduledThreadPoolExecutor(20);

        mIsStationary = true;
        mNotificationManager.cancel(R.layout.main);
    }
}

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 .ja v a2s . c  o m

    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:com.quran.labs.androidquran.service.AudioService.java

/**
 * Configures service as a foreground service. A foreground service
 * is a service that's doing something the user is actively aware of
 * (such as playing music), and must appear to the user as a notification.
 * That's why we create the notification here.
 *//*from  w w  w  .j a va  2 s. c  om*/
private void setUpAsForeground(String text) {
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            new Intent(getApplicationContext(), PagerActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
    mNotification = new Notification();
    mNotification.tickerText = text;
    mNotification.icon = R.drawable.icon;
    mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
    mNotification.setLatestEventInfo(getApplicationContext(), mNotificationName, text, pi);
    startForeground(NOTIFICATION_ID, mNotification);
}

From source file:RhodesService.java

private void updateDownloadNotification(String url, int totalBytes, int currentBytes) {
    Context context = RhodesActivity.getContext();

    Notification n = new Notification();
    n.icon = android.R.drawable.stat_sys_download;
    n.flags |= Notification.FLAG_ONGOING_EVENT;

    RemoteViews expandedView = new RemoteViews(context.getPackageName(),
            R.layout.status_bar_ongoing_event_progress_bar);

    StringBuilder newUrl = new StringBuilder();
    if (url.length() < 17)
        newUrl.append(url);//from w  w  w  .  j  a  v  a 2s.  com
    else {
        newUrl.append(url.substring(0, 7));
        newUrl.append("...");
        newUrl.append(url.substring(url.length() - 7, url.length()));
    }
    expandedView.setTextViewText(R.id.title, newUrl.toString());

    StringBuffer downloadingText = new StringBuffer();
    if (totalBytes > 0) {
        long progress = currentBytes * 100 / totalBytes;
        downloadingText.append(progress);
        downloadingText.append('%');
    }
    expandedView.setTextViewText(R.id.progress_text, downloadingText.toString());
    expandedView.setProgressBar(R.id.progress_bar, totalBytes < 0 ? 100 : totalBytes, currentBytes,
            totalBytes < 0);
    n.contentView = expandedView;

    Intent intent = new Intent(ACTION_ASK_CANCEL_DOWNLOAD);
    n.contentIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    intent = new Intent(ACTION_CANCEL_DOWNLOAD);
    n.deleteIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    mNM.notify(DOWNLOAD_PACKAGE_ID, n);
}

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

/**
 * @author Tobias Rho/*from  www .ja v  a  2s . c  o m*/
 */
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:com.adityarathi.muo.services.AudioPlaybackService.java

/**
 * Builds and returns a fully constructed Notification for devices 
 * on Jelly Bean and above (API 16+)./*w w  w . ja  va 2 s. com*/
 */
@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;
}