Example usage for android.content Intent FLAG_ACTIVITY_SINGLE_TOP

List of usage examples for android.content Intent FLAG_ACTIVITY_SINGLE_TOP

Introduction

In this page you can find the example usage for android.content Intent FLAG_ACTIVITY_SINGLE_TOP.

Prototype

int FLAG_ACTIVITY_SINGLE_TOP

To view the source code for android.content Intent FLAG_ACTIVITY_SINGLE_TOP.

Click Source Link

Document

If set, the activity will not be launched if it is already running at the top of the history stack.

Usage

From source file:com.kth.baasio.sample.gcm.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *///from www  . j a va2 s  . c o  m
private static void generateNotification(Context context, String message) {
    BaasioPayload msg = JsonUtils.parse(message, BaasioPayload.class);
    if (ObjectUtils.isEmpty(msg)) {
        return;
    }

    String alert = "";
    if (!ObjectUtils.isEmpty(msg.getAlert())) {
        alert = msg.getAlert().replace("\\r\\n", "\n");
    }

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, BaasioMainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(context).setWhen(when).setSmallIcon(icon)
            .setContentTitle(context.getString(R.string.app_name)).setContentText(alert)
            .setContentIntent(intent).setTicker(alert).setAutoCancel(true).getNotification();

    notificationManager.notify(0, notification);
}

From source file:edu.cmu.cylab.starslinger.util.NotificationBroadcastReceiver.java

@SuppressWarnings("deprecation")
public static void doUnseenMessagesNotification(Context ctx, int msgCount, boolean giveNotificationFeedback)
        throws OutOfMemoryError {
    long when = System.currentTimeMillis(); // notification time

    // To create a status bar notification:
    // Get a reference to the NotificationManager:
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) ctx.getSystemService(ns);

    String tickerText = ctx.getString(R.string.title_NotifyFileAvailable);

    String contentTitle = String.format(Locale.getDefault(), "%s (%d)", ctx.getString(R.string.app_name),
            msgCount);/*from w  w  w  .j av  a2 s .com*/
    String contentText = String.format(ctx.getString(R.string.label_ClickForNMsgs), msgCount);

    Intent intent = makeMessagesNotificationIntent(ctx);
    intent.setFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // the next two lines initialize the Notification, using the
    // configurations above
    int visibleMsgCount = msgCount != 1 ? msgCount : 0;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)//
            .setContentIntent(contentIntent)//
            .setSmallIcon(R.drawable.ic_stat_notify_msg)//
            .setTicker(tickerText)//
            .setWhen(when)//
            .setAutoCancel(true)//
            .setContentTitle(contentTitle)//
            .setContentText(contentText)//
            .setNumber(visibleMsgCount)//
            .setVisibility(NotificationCompat.VISIBILITY_SECRET);

    try {
        builder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_launcher));
    } catch (OutOfMemoryError e) {
        // ignore icon when out of memory
    }

    // set notification alerts based on user preferences
    int defaults = 0;
    if (SafeSlingerPrefs.getNotificationVibrate() && giveNotificationFeedback) {
        defaults |= Notification.DEFAULT_VIBRATE;
    }
    String ringtoneStr = SafeSlingerPrefs.getNotificationRingTone();
    builder.setSound(
            TextUtils.isEmpty(ringtoneStr) || !giveNotificationFeedback ? null : Uri.parse(ringtoneStr));
    defaults |= Notification.DEFAULT_LIGHTS;
    builder.setDefaults(defaults);

    Notification n = builder.build();

    // total messages seen...
    n.number = visibleMsgCount; // API <11
    if (msgCount != 1) {
        // cancel old one since we want to avoid the "1" when updating
        // number
        nm.cancel(HomeActivity.NOTIFY_NEW_MSG_ID);
    }

    // Pass the Notification to the NotificationManager:
    nm.notify(HomeActivity.NOTIFY_NEW_MSG_ID, n);
}

From source file:com.meetingcpp.sched.gcm.command.AnnouncementCommand.java

private void displayNotification(Context context, String message) {
    LOGI(TAG, "Displaying notification: " + message);
    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(0,
            new NotificationCompat.Builder(context).setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_stat_notification).setTicker(message)
                    .setContentTitle(context.getString(R.string.app_name)).setContentText(message)
                    //.setColor(context.getResources().getColor(R.color.theme_primary))
                    // Note: setColor() is available in the support lib v21+.
                    // We commented it out because we want the source to compile
                    // against support lib v20. If you are using support lib
                    // v21 or above on Android L, uncomment this line.
                    .setContentIntent(PendingIntent.getActivity(context, 0,
                            new Intent(context, MyScheduleActivity.class).setFlags(
                                    Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                            0))// w w w . ja  v  a 2 s  .  c  o  m
                    .setAutoCancel(true).build());
}

From source file:com.meiste.greg.ptw.RaceActivity.java

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        final Intent homeIntent = new Intent(this, MainActivity.class);
        homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(homeIntent);//from  ww  w  . j ava  2 s.  c  om

        if (getIntent().getBooleanExtra(INTENT_ALARM, false)) {
            // Finish activity so back button works as expected
            finish();
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}

From source file:com.example.castremotedisplay.ndk.local.LocalActivity.java

private void startRemoteDisplayService(CastDevice castDevice) {
    Intent intent = new Intent(LocalActivity.this, LocalActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent notificationPendingIntent = PendingIntent.getActivity(LocalActivity.this, 0, intent, 0);

    CastRemoteDisplayLocalService.NotificationSettings settings = new CastRemoteDisplayLocalService.NotificationSettings.Builder()
            .setNotificationPendingIntent(notificationPendingIntent).build();

    CastRemoteDisplayLocalService.startService(getApplicationContext(), RemoteDisplayService.class,
            getString(R.string.app_id), castDevice, settings, new CastRemoteDisplayLocalService.Callbacks() {
                @Override//from w w w  .java2s . c o  m
                public void onServiceCreated(CastRemoteDisplayLocalService service) {
                    Log.d(TAG, "onServiceCreated");
                }

                @Override
                public void onRemoteDisplaySessionStarted(CastRemoteDisplayLocalService service) {
                    Log.d(TAG, "onServiceStarted");
                }

                @Override
                public void onRemoteDisplaySessionError(Status errorReason) {
                    int code = errorReason.getStatusCode();
                    Log.d(TAG, "onServiceError: " + errorReason.getStatusCode());
                    initError();

                    LocalActivity.this.finish();
                }
            });
}

From source file:com.google.samples.apps.iosched.gcm.command.AnnouncementCommand.java

private void displayNotification(Context context, String message) {
    LOGI(TAG, "Displaying notification: " + message);
    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(0,
            new NotificationCompat.Builder(context).setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_stat_notification).setTicker(message)
                    .setContentTitle(context.getString(R.string.app_name)).setContentText(message)
                    //.setColor(context.getResources().getColor(R.color.theme_primary))
                    // Note: setColor() is available in the support lib v21+.
                    // We commented it out because we want the source to compile 
                    // against support lib v20. If you are using support lib
                    // v21 or above on Android L, uncomment this line.
                    .setContentIntent(PendingIntent.getActivity(context, 0,
                            new Intent(context, MyScheduleActivity.class).setFlags(
                                    Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                            0))/*from   ww  w. j a  v a 2 s  . co  m*/
                    .setAutoCancel(true).build());
}

From source file:com.arellomobile.android.push.PushGCMIntentService.java

private static void generateNotification(Context context, Intent intent, Handler handler) {
    Bundle extras = intent.getExtras();/*  ww w. j av a 2s  .  co  m*/
    if (extras == null) {
        return;
    }

    extras.putBoolean("foregroud", GeneralUtils.isAppOnForeground(context));

    String title = (String) extras.get("title");
    String link = (String) extras.get("l");

    // empty message with no data
    Intent notifyIntent;
    if (link != null) {
        // we want main app class to be launched
        notifyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
        notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    } else {
        notifyIntent = new Intent(context, PushHandlerActivity.class);
        notifyIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // pass all bundle
        notifyIntent.putExtra("pushBundle", extras);
    }

    // first string will appear on the status bar once when message is added
    CharSequence appName = context.getPackageManager().getApplicationLabel(context.getApplicationInfo());
    if (null == appName) {
        appName = "";
    }

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationFactory notificationFactory;

    //is this banner notification?
    String bannerUrl = (String) extras.get("b");

    //also check that notification layout has been placed in layout folder
    int layoutId = context.getResources().getIdentifier(BannerNotificationFactory.sNotificationLayout, "layout",
            context.getPackageName());

    if (layoutId != 0 && bannerUrl != null) {
        notificationFactory = new BannerNotificationFactory(context, extras, appName.toString(), title,
                PushManager.sSoundType, PushManager.sVibrateType);
    } else {
        notificationFactory = new SimpleNotificationFactory(context, extras, appName.toString(), title,
                PushManager.sSoundType, PushManager.sVibrateType);
    }
    notificationFactory.generateNotification();
    notificationFactory.addSoundAndVibrate();
    notificationFactory.addCancel();

    Notification notification = notificationFactory.getNotification();

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

    if (mSimpleNotification) {
        manager.notify(PushManager.MESSAGE_ID, notification);
    } else {
        manager.notify(PushManager.MESSAGE_ID++, notification);
    }

    generateBroadcast(context, extras);
}

From source file:im.neon.util.NotificationUtils.java

/**
 * Build an incoming call notification./*from www  . ja  va 2s . c o  m*/
 * This notification starts the VectorHomeActivity which is in charge of centralizing the incoming call flow.
 * @param context the context.
 * @param roomName the room name in which the call is pending.
 * @param matrixId the matrix id
 * @param callId the call id.
 * @return the call notification.
 */
public static Notification buildIncomingCallNotification(Context context, String roomName, String matrixId,
        String callId) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setWhen(System.currentTimeMillis());

    builder.setContentTitle(roomName);
    builder.setContentText(context.getString(R.string.incoming_call));
    builder.setSmallIcon(R.drawable.incoming_call_notification_transparent);

    // clear the activity stack to home activity
    Intent intent = new Intent(context, VectorHomeActivity.class);
    intent.setFlags(
            Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(VectorHomeActivity.EXTRA_CALL_SESSION_ID, matrixId);
    intent.putExtra(VectorHomeActivity.EXTRA_CALL_ID, callId);

    // Recreate the back stack
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context).addParentStack(VectorHomeActivity.class)
            .addNextIntent(intent);

    // android 4.3 issue
    // use a generator for the private requestCode.
    // When using 0, the intent is not created/launched when the user taps on the notification.
    //
    PendingIntent pendingIntent = stackBuilder.getPendingIntent((new Random()).nextInt(1000),
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);

    Notification n = builder.build();
    n.flags |= Notification.FLAG_SHOW_LIGHTS;
    n.defaults |= Notification.DEFAULT_LIGHTS;

    return n;
}

From source file:lc.buyplus.gcm.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received./*from   www . j a  va2s  . co  m*/
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.buypluslogo).setContentTitle("BuyPlus Message").setContentText(message)
            .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:edu.berkeley.boinc.client.NoticeNotification.java

public NoticeNotification(Context ctx) {
    this.context = ctx;
    this.store = new PersistentStorage(ctx);
    this.nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationId = context.getResources().getInteger(R.integer.notice_notification_id);
    Intent intent = new Intent(context, BOINCActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra("targetFragment", R.string.tab_notices);
    contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}