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.google.samples.apps.iosched.gcm.command.NotificationCommand.java

private void processCommand(Context context, NotificationCommandModel command) {
    // Check format
    if (!"1.0.00".equals(command.format)) {
        LOGW(TAG, "GCM notification command has unrecognized format: " + command.format);
        return;//from w w  w  . j  a va 2  s . c  o m
    }

    // Check app version
    if (!TextUtils.isEmpty(command.minVersion) || !TextUtils.isEmpty(command.maxVersion)) {
        LOGD(TAG, "Command has version range.");
        int minVersion = 0;
        int maxVersion = Integer.MAX_VALUE;
        try {
            if (!TextUtils.isEmpty(command.minVersion)) {
                minVersion = Integer.parseInt(command.minVersion);
            }
            if (!TextUtils.isEmpty(command.maxVersion)) {
                maxVersion = Integer.parseInt(command.maxVersion);
            }
            LOGD(TAG, "Version range: " + minVersion + " - " + maxVersion);
            PackageInfo pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            LOGD(TAG, "My version code: " + pinfo.versionCode);
            if (pinfo.versionCode < minVersion) {
                LOGD(TAG, "Skipping command because our version is too old, " + pinfo.versionCode + " < "
                        + minVersion);
                return;
            }
            if (pinfo.versionCode > maxVersion) {
                LOGD(TAG, "Skipping command because our version is too new, " + pinfo.versionCode + " > "
                        + maxVersion);
                return;
            }
        } catch (NumberFormatException ex) {
            LOGE(TAG,
                    "Version spec badly formatted: min=" + command.minVersion + ", max=" + command.maxVersion);
            return;
        } catch (Exception ex) {
            LOGE(TAG, "Unexpected problem doing version check.", ex);
            return;
        }
    }

    // Check if we are the right audience
    LOGD(TAG, "Checking audience: " + command.audience);
    if ("remote".equals(command.audience)) {
        if (SettingsUtils.isAttendeeAtVenue(context)) {
            LOGD(TAG, "Ignoring notification because audience is remote and attendee is on-site");
            return;
        } else {
            LOGD(TAG, "Relevant (attendee is remote).");
        }
    } else if ("local".equals(command.audience)) {
        if (!SettingsUtils.isAttendeeAtVenue(context)) {
            LOGD(TAG, "Ignoring notification because audience is on-site and attendee is remote.");
            return;
        } else {
            LOGD(TAG, "Relevant (attendee is local).");
        }
    } else if ("all".equals(command.audience)) {
        LOGD(TAG, "Relevant (audience is 'all').");
    } else {
        LOGE(TAG, "Invalid audience on GCM notification command: " + command.audience);
        return;
    }

    // Check if it expired
    Date expiry = command.expiry == null ? null : TimeUtils.parseTimestamp(command.expiry);
    if (expiry == null) {
        LOGW(TAG, "Failed to parse expiry field of GCM notification command: " + command.expiry);
        return;
    } else if (expiry.getTime() < UIUtils.getCurrentTime(context)) {
        LOGW(TAG, "Got expired GCM notification command. Expiry: " + expiry.toString());
        return;
    } else {
        LOGD(TAG, "Message is still valid (expiry is in the future: " + expiry.toString() + ")");
    }

    // decide the intent that will be fired when the user clicks the notification
    Intent intent;
    if (TextUtils.isEmpty(command.dialogText)) {
        // notification leads directly to the URL, no dialog
        if (TextUtils.isEmpty(command.url)) {
            intent = new Intent(context, MyScheduleActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        } else {
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(command.url));
        }
    } else {
        // use a dialog
        intent = new Intent(context, MyScheduleActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_TITLE,
                command.dialogTitle == null ? "" : command.dialogTitle);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_MESSAGE,
                command.dialogText == null ? "" : command.dialogText);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_YES,
                command.dialogYes == null ? "OK" : command.dialogYes);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_NO, command.dialogNo == null ? "" : command.dialogNo);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_URL, command.url == null ? "" : command.url);
    }

    final String title = TextUtils.isEmpty(command.title) ? context.getString(R.string.app_name)
            : command.title;
    final String message = TextUtils.isEmpty(command.message) ? "" : command.message;

    // fire the notification
    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(0,
            new NotificationCompat.Builder(context).setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_stat_notification).setTicker(command.message)
                    .setContentTitle(title).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, intent, PendingIntent.FLAG_CANCEL_CURRENT))
                    .setAutoCancel(true).build());
}

From source file:com.google.samples.apps.sergio.gcm.command.NotificationCommand.java

private void processCommand(Context context, NotificationCommandModel command) {
    // Check format
    if (!"1.0.00".equals(command.format)) {
        LOGW(TAG, "GCM notification command has unrecognized format: " + command.format);
        return;/* w  w w .ja  v a2 s  . c  om*/
    }

    // Check app version
    if (!TextUtils.isEmpty(command.minVersion) || !TextUtils.isEmpty(command.maxVersion)) {
        LOGD(TAG, "Command has version range.");
        int minVersion = 0;
        int maxVersion = Integer.MAX_VALUE;
        try {
            if (!TextUtils.isEmpty(command.minVersion)) {
                minVersion = Integer.parseInt(command.minVersion);
            }
            if (!TextUtils.isEmpty(command.maxVersion)) {
                maxVersion = Integer.parseInt(command.maxVersion);
            }
            LOGD(TAG, "Version range: " + minVersion + " - " + maxVersion);
            PackageInfo pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            LOGD(TAG, "My version code: " + pinfo.versionCode);
            if (pinfo.versionCode < minVersion) {
                LOGD(TAG, "Skipping command because our version is too old, " + pinfo.versionCode + " < "
                        + minVersion);
                return;
            }
            if (pinfo.versionCode > maxVersion) {
                LOGD(TAG, "Skipping command because our version is too new, " + pinfo.versionCode + " > "
                        + maxVersion);
                return;
            }
        } catch (NumberFormatException ex) {
            LOGE(TAG,
                    "Version spec badly formatted: min=" + command.minVersion + ", max=" + command.maxVersion);
            return;
        } catch (Exception ex) {
            LOGE(TAG, "Unexpected problem doing version check.", ex);
            return;
        }
    }

    // Check if we are the right audience
    LOGD(TAG, "Checking audience: " + command.audience);
    if ("remote".equals(command.audience)) {
        if (PrefUtils.isAttendeeAtVenue(context)) {
            LOGD(TAG, "Ignoring notification because audience is remote and attendee is on-site");
            return;
        } else {
            LOGD(TAG, "Relevant (attendee is remote).");
        }
    } else if ("local".equals(command.audience)) {
        if (!PrefUtils.isAttendeeAtVenue(context)) {
            LOGD(TAG, "Ignoring notification because audience is on-site and attendee is remote.");
            return;
        } else {
            LOGD(TAG, "Relevant (attendee is local).");
        }
    } else if ("all".equals(command.audience)) {
        LOGD(TAG, "Relevant (audience is 'all').");
    } else {
        LOGE(TAG, "Invalid audience on GCM notification command: " + command.audience);
        return;
    }

    // Check if it expired
    Date expiry = command.expiry == null ? null : TimeUtils.parseTimestamp(command.expiry);
    if (expiry == null) {
        LOGW(TAG, "Failed to parse expiry field of GCM notification command: " + command.expiry);
        return;
    } else if (expiry.getTime() < UIUtils.getCurrentTime(context)) {
        LOGW(TAG, "Got expired GCM notification command. Expiry: " + expiry.toString());
        return;
    } else {
        LOGD(TAG, "Message is still valid (expiry is in the future: " + expiry.toString() + ")");
    }

    // decide the intent that will be fired when the user clicks the notification
    Intent intent;
    if (TextUtils.isEmpty(command.dialogText)) {
        // notification leads directly to the URL, no dialog
        if (TextUtils.isEmpty(command.url)) {
            intent = new Intent(context, MyScheduleActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        } else {
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(command.url));
        }
    } else {
        // use a dialog
        intent = new Intent(context, MyScheduleActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
                        | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_TITLE,
                command.dialogTitle == null ? "" : command.dialogTitle);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_MESSAGE,
                command.dialogText == null ? "" : command.dialogText);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_YES,
                command.dialogYes == null ? "OK" : command.dialogYes);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_NO, command.dialogNo == null ? "" : command.dialogNo);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_URL, command.url == null ? "" : command.url);
    }

    final String title = TextUtils.isEmpty(command.title) ? context.getString(R.string.app_name)
            : command.title;
    final String message = TextUtils.isEmpty(command.message) ? "" : command.message;

    // fire the notification
    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(0,
            new NotificationCompat.Builder(context).setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_stat_notification).setTicker(command.message)
                    .setContentTitle(title).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, intent, PendingIntent.FLAG_CANCEL_CURRENT))
                    .setAutoCancel(true).build());
}

From source file:com.onshift.mobile.ExternalReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int notificationCount = 1;

    Bundle extras = intent.getExtras();/*from www  .  j av  a2  s .c o  m*/
    StringBuilder payload = new StringBuilder();
    StringBuilder messagePayload = new StringBuilder();
    StringBuilder message = new StringBuilder();

    Gson gson = new Gson();
    msgv1 = new PushNotificationMessage_v1();
    msg = new PushNotificationMessage();

    SharedPreferences settings = context.getSharedPreferences("mysettings", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    String UserID = settings.getString("UserID", "");

    Boolean registrationmsg = false;
    Boolean displayNotification = false;

    if (extras != null) {
        for (String key : extras.keySet()) {
            if (key.equals("registration_id")) {
                Log.i("key: ", key);
                Log.i("value: ", extras.getString(key));
                payload.append(String.format("%s=%s", key, extras.getString(key)) + '\n');
                MainActivity.sendRegistrationToWebview(extras.getString(key));
                registrationmsg = true;
            }

            if (key.equals("error")) {
                registrationmsg = true;
            }

            if (extras.containsKey("message")) {
                if ((key.equals("message")) || (key.equals("message_type_id")) || (key.equals("shift_id"))
                        || (key.equals("id")) || (key.equals("recipient_id")) || (key.equals("badge"))) {
                    if (key.equals("message")) {
                        message.append(String.format("%s", extras.getString(key)));
                        msgv1.set_message(extras.getString(key));
                    }

                    if (key.equals("message_type_id")) {
                        msgv1.set_messagetypeid(extras.getString(key));
                    }

                    if (key.equals("id")) {
                        msgv1.set_messageid(extras.getString(key));
                    }

                    if (key.equals("shift_id")) {
                        msgv1.set_shiftid(extras.getString(key));
                    }

                    if (key.equals("badge")) {
                        msgv1.set_badge(extras.getString(key));
                        try {
                            notificationCount = Integer.parseInt(extras.getString(key));
                        } catch (NumberFormatException nfe) {

                        }
                    }

                    if (key.equals("recipient_id")) {
                        if (extras.getString(key).equals(UserID)) {
                            displayNotification = true;
                        }
                    }
                }
            }

            if (extras.containsKey("msg")) {
                if ((key.equals("msg")) || (key.equals("msg_type")) || (key.equals("id")) || (key.equals("rid"))
                        || (key.equals("badge"))) {
                    if (key.equals("msg")) {
                        message.append(String.format("%s", extras.getString(key)));
                        msg.set_message(extras.getString(key));
                    }

                    if (key.equals("msg_type")) {
                        msg.set_msgtype(extras.getString(key));
                    }

                    if (key.equals("id")) {
                        msg.set_messageid(extras.getString(key));
                    }

                    if (key.equals("badge")) {
                        msg.set_badge(extras.getString(key));
                        try {
                            notificationCount = Integer.parseInt(extras.getString(key));
                        } catch (NumberFormatException nfe) {

                        }
                    }

                    if (key.equals("rid")) {
                        if (extras.getString(key).equals(UserID)) {
                            displayNotification = true;
                        }
                    }
                }
            }
        }

        if (msgv1.get_message() != null) {
            messagePayload.append(gson.toJson(msgv1));
        }

        if (msg.get_message() != null) {
            messagePayload.append(gson.toJson(msg));
        }

        try {
            if (messagePayload.toString() != null) {
                if (!registrationmsg) {
                    if (displayNotification) {
                        MainActivity.sendForegroundNotificationToWebview(messagePayload.toString());
                    }
                }
            }
        } catch (Exception e) {
        }
    }

    if (notificationCount > 1) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher).setContentTitle("OnShift Mobile")
                .setContentText(String.valueOf(notificationCount) + MESSAGETEMPLATE)
                .setDefaults(Notification.DEFAULT_VIBRATE);

        // Intent resultIntent = new Intent(context, MainActivity.class);
        Intent resultIntent = new Intent(context, UniversalLoginActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (messagePayload.toString() != null) {
            resultIntent.putExtra("message", messagePayload.toString());
            editor.putString("PendingMessage", messagePayload.toString());
            editor.commit();
        }

        // The stack builder object will contain an artificial back stack
        // for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out
        // of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(UniversalLoginActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        if (displayNotification) {
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        }
    } else {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher).setContentTitle("OnShift Mobile").setContentText(message)
                .setDefaults(Notification.DEFAULT_VIBRATE);

        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, UniversalLoginActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (messagePayload.toString() != null) {
            resultIntent.putExtra("message", messagePayload.toString());
            editor.putString("PendingMessage", messagePayload.toString());
            editor.commit();
        }
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(UniversalLoginActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        if (!registrationmsg) {
            if (displayNotification) {
                mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
            }
        }
    }
}

From source file:ca.mudar.snoozy.receiver.PowerConnectionReceiver.java

private void notify(Context context, boolean isConnectedPower, boolean hasVibration, boolean hasSound,
        int notifyCount) {
    final Resources res = context.getResources();

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_notify).setContentTitle(res.getString(R.string.notify_content_title))
            .setAutoCancel(true);/*w  w w. j  a v a2 s. com*/

    if (notifyCount == 1) {
        final int resContentTextSingle = (isConnectedPower ? R.string.notify_content_text_single_on
                : R.string.notify_content_text_single_off);
        mBuilder.setContentText(res.getString(resContentTextSingle));
    } else {
        mBuilder.setNumber(notifyCount).setContentText(res.getString(R.string.notify_content_text_multi));
    }

    if (hasVibration && hasSound) {
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND);
    } else if (hasVibration) {
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    } else if (hasSound) {
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    }

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, MainActivity.class);
    final Bundle extras = new Bundle();
    extras.putBoolean(Const.IntentExtras.INCREMENT_NOTIFY_GROUP, true);
    extras.putBoolean(Const.IntentExtras.RESET_NOTIFY_NUMBER, true);
    resultIntent.putExtras(extras);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    mBuilder.setContentIntent(
            PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    Intent receiverIntent = new Intent(context, PowerConnectionReceiver.class);
    receiverIntent.setAction(Const.IntentActions.NOTIFY_DELETE);
    PendingIntent deleteIntent = PendingIntent.getBroadcast(context, Const.RequestCodes.RESET_NOTIFY_NUMBER,
            receiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setDeleteIntent(deleteIntent);

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

    // NOTIFY_ID allows updates to the notification later on.
    mNotificationManager.notify(Const.NOTIFY_ID, mBuilder.build());
}

From source file:com.jaspersoft.android.jaspermobile.dialog.PasswordDialogFragment.java

private void tryToLogin(String password) {
    mCheckPasswordUseCase.execute(password, new Subscriber<Void>() {
        @Override//from  ww  w  . j  a  va 2  s. co  m
        public void onStart() {
            showLoader();
        }

        @Override
        public void onCompleted() {
            hideLoader();
        }

        @Override
        public void onError(Throwable e) {
            if (e instanceof ServiceException) {
                ServiceException serviceException = (ServiceException) e;
                int code = serviceException.code();
                if (code == StatusCodes.AUTHORIZATION_ERROR) {
                    showError(getString(R.string.r_error_incorrect_credentials));
                }
            } else {
                showError(e.getLocalizedMessage());
            }
            hideLoader();
        }

        @Override
        public void onNext(Void item) {
            dismiss();

            Intent restartIntent = NavigationActivity_.intent(getActivity()).get();
            restartIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            getActivity().finish();
            getActivity().startActivity(restartIntent);
        }
    });
}

From source file:com.tananaev.passportreader.MainActivity.java

@Override
protected void onResume() {
    super.onResume();

    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    Intent intent = new Intent(getApplicationContext(), this.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    String[][] filter = new String[][] { new String[] { "android.nfc.tech.IsoDep" } };
    adapter.enableForegroundDispatch(this, pendingIntent, null, filter);
}

From source file:com.classiqo.nativeandroid_32bitz.MediaNotificationManager.java

private PendingIntent createContentIntent(MediaDescriptionCompat description) {
    Intent openUI = new Intent(mService, MusicPlayerActivity.class);
    openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    openUI.putExtra(MusicPlayerActivity.EXTRA_START_FULLSCREEN, true);

    if (description != null) {
        openUI.putExtra(MusicPlayerActivity.EXTRA_CURRENT_MEDIA_DESCRIPTION, description);
    }/*w  ww. ja  v a2  s .c  o m*/

    return PendingIntent.getActivity(mService, REQUEST_CODE, openUI, PendingIntent.FLAG_CANCEL_CURRENT);
}

From source file:com.nps.micro.UsbService.java

/**
 * Show a notification while this service is running.
 *///from   ww w.j av a  2s  .  c  om
private void showNotification() {
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    Notification notification = new NotificationCompat.Builder(getApplicationContext())
            .setContentIntent(PendingIntent.getActivity(this, 0, intent, 0))
            .setSmallIcon(R.drawable.ic_launcher).setContentText(getText(R.string.local_service_running))
            .setContentTitle(getText(R.string.local_service_notification)).build();
    notificationManager.notify(NOTIFICATION, notification);
}

From source file:com.mediageoloc.ata.drawer.DrawerActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    switch (position) {
    case 0://from w  w w  .j a va  2  s  . co  m
        if (this.getClass() != TakeMediaActivity.class) {
            Intent intent = new Intent(this, TakeMediaActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        }
        break;
    case 1:
        if (this.getClass() != HistoricMediaEtsyActivity.class) {
            Intent intent = new Intent(this, HistoricMediaEtsyActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        }
        break;
    case 2:
        if (this.getClass() != UserAccountActivity.class) {
            Intent intent = new Intent(this, UserAccountActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        }
        break;
    case 3:
        if (this.getClass() != MapActivity.class) {
            Intent intent = new Intent(this, MapActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        }
        break;
    case 4:
        if (this.getClass() != UsersActivity.class) {
            Intent intent = new Intent(this, UsersActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        }
        break;
    case 5:
        if (this.getClass() != FollowersActivity.class) {
            Intent intent = new Intent(this, FollowersActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        }
        break;
    default:
    }
}

From source file:com.saarang.samples.apps.iosched.gcm.command.NotificationCommand.java

private void processCommand(Context context, NotificationCommandModel command) {
    // Check format
    if (!"1.0.00".equals(command.format)) {
        LogUtils.LOGW(TAG, "GCM notification command has unrecognized format: " + command.format);
        return;/*from  ww w.  j  a v a 2  s . c om*/
    }

    // Check app version
    if (!TextUtils.isEmpty(command.minVersion) || !TextUtils.isEmpty(command.maxVersion)) {
        LogUtils.LOGD(TAG, "Command has version range.");
        int minVersion = 0;
        int maxVersion = Integer.MAX_VALUE;
        try {
            if (!TextUtils.isEmpty(command.minVersion)) {
                minVersion = Integer.parseInt(command.minVersion);
            }
            if (!TextUtils.isEmpty(command.maxVersion)) {
                maxVersion = Integer.parseInt(command.maxVersion);
            }
            LogUtils.LOGD(TAG, "Version range: " + minVersion + " - " + maxVersion);
            PackageInfo pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            LogUtils.LOGD(TAG, "My version code: " + pinfo.versionCode);
            if (pinfo.versionCode < minVersion) {
                LogUtils.LOGD(TAG, "Skipping command because our version is too old, " + pinfo.versionCode
                        + " < " + minVersion);
                return;
            }
            if (pinfo.versionCode > maxVersion) {
                LogUtils.LOGD(TAG, "Skipping command because our version is too new, " + pinfo.versionCode
                        + " > " + maxVersion);
                return;
            }
        } catch (NumberFormatException ex) {
            LogUtils.LOGE(TAG,
                    "Version spec badly formatted: min=" + command.minVersion + ", max=" + command.maxVersion);
            return;
        } catch (Exception ex) {
            LogUtils.LOGE(TAG, "Unexpected problem doing version check.", ex);
            return;
        }
    }

    // Check if we are the right audience
    LogUtils.LOGD(TAG, "Checking audience: " + command.audience);
    if ("remote".equals(command.audience)) {
        if (PrefUtils.isAttendeeAtVenue(context)) {
            LogUtils.LOGD(TAG, "Ignoring notification because audience is remote and attendee is on-site");
            return;
        } else {
            LogUtils.LOGD(TAG, "Relevant (attendee is remote).");
        }
    } else if ("local".equals(command.audience)) {
        if (!PrefUtils.isAttendeeAtVenue(context)) {
            LogUtils.LOGD(TAG, "Ignoring notification because audience is on-site and attendee is remote.");
            return;
        } else {
            LogUtils.LOGD(TAG, "Relevant (attendee is local).");
        }
    } else if ("all".equals(command.audience)) {
        LogUtils.LOGD(TAG, "Relevant (audience is 'all').");
    } else {
        LogUtils.LOGE(TAG, "Invalid audience on GCM notification command: " + command.audience);
        return;
    }

    // Check if it expired
    Date expiry = command.expiry == null ? null : TimeUtils.parseTimestamp(command.expiry);
    if (expiry == null) {
        LogUtils.LOGW(TAG, "Failed to parse expiry field of GCM notification command: " + command.expiry);
        return;
    } else if (expiry.getTime() < UIUtils.getCurrentTime(context)) {
        LogUtils.LOGW(TAG, "Got expired GCM notification command. Expiry: " + expiry.toString());
        return;
    } else {
        LogUtils.LOGD(TAG, "Message is still valid (expiry is in the future: " + expiry.toString() + ")");
    }

    // decide the intent that will be fired when the user clicks the notification
    Intent intent;
    if (TextUtils.isEmpty(command.dialogText)) {
        // notification leads directly to the URL, no dialog
        if (TextUtils.isEmpty(command.url)) {
            intent = new Intent(context, MyScheduleActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        } else {
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(command.url));
        }
    } else {
        // use a dialog
        intent = new Intent(context, MyScheduleActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
                        | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_TITLE,
                command.dialogTitle == null ? "" : command.dialogTitle);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_MESSAGE,
                command.dialogText == null ? "" : command.dialogText);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_YES,
                command.dialogYes == null ? "OK" : command.dialogYes);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_NO, command.dialogNo == null ? "" : command.dialogNo);
        intent.putExtra(MyScheduleActivity.EXTRA_DIALOG_URL, command.url == null ? "" : command.url);
    }

    final String title = TextUtils.isEmpty(command.title)
            ? context.getString(com.saarang.samples.apps.iosched.R.string.app_name)
            : command.title;
    final String message = TextUtils.isEmpty(command.message) ? "" : command.message;

    // fire the notification
    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(0,
            new NotificationCompat.Builder(context).setWhen(System.currentTimeMillis())
                    .setSmallIcon(com.saarang.samples.apps.iosched.R.drawable.ic_stat_notification)
                    .setTicker(command.message).setContentTitle(title).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, intent, PendingIntent.FLAG_CANCEL_CURRENT))
                    .setAutoCancel(true).build());
}