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:im.neon.services.EventStreamService.java

/**
 * @return the polling thread listener notification
 *///from   www.  java 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:com.piusvelte.taplock.client.core.TapLockSettings.java

@Override
protected void onListItemClick(ListView list, final View view, int position, final long id) {
    super.onListItemClick(list, view, position, id);
    mDialog = new AlertDialog.Builder(TapLockSettings.this)
            .setItems(R.array.actions_entries, new DialogInterface.OnClickListener() {
                @Override//  w w  w . j  a  v a  2  s.co m
                public void onClick(DialogInterface dialog, int which) {
                    String action = getResources().getStringArray(R.array.actions_values)[which];
                    int deviceIdx = (int) id;
                    JSONObject deviceJObj = mDevices.get(deviceIdx);
                    dialog.cancel();
                    if (ACTION_UNLOCK.equals(action) || ACTION_LOCK.equals(action)
                            || ACTION_TOGGLE.equals(action)) {
                        String name = "uknown";
                        try {
                            name = deviceJObj.getString(KEY_NAME);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        startActivity(TapLock.getPackageIntent(getApplicationContext(), TapLockToggle.class)
                                .setAction(action).putExtra(EXTRA_DEVICE_NAME, name));
                    } else if (ACTION_TAG.equals(action)) {
                        // write the device to a tag
                        mInWriteMode = true;
                        try {
                            mNfcAdapter.enableForegroundDispatch(TapLockSettings.this,
                                    PendingIntent.getActivity(TapLockSettings.this, 0,
                                            new Intent(TapLockSettings.this, TapLockSettings.this.getClass())
                                                    .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra(
                                                            EXTRA_DEVICE_NAME, deviceJObj.getString(KEY_NAME)),
                                            0),
                                    new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED) },
                                    null);
                        } catch (JSONException e) {
                            Log.e(TAG, e.getMessage());
                        }
                        Toast.makeText(TapLockSettings.this, "Touch tag", Toast.LENGTH_LONG).show();
                    } else if (ACTION_REMOVE.equals(action)) {
                        mDevices.remove(deviceIdx);
                        storeDevices();
                    } else if (ACTION_PASSPHRASE.equals(action))
                        setPassphrase(deviceIdx);
                    else if (ACTION_COPY_DEVICE_URI.equals(action)) {
                        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                        ClipData clip;
                        try {
                            clip = ClipData.newPlainText(getString(R.string.app_name), String
                                    .format(getString(R.string.device_uri), deviceJObj.get(EXTRA_DEVICE_NAME)));
                            clipboard.setPrimaryClip(clip);
                            Toast.makeText(TapLockSettings.this, "copied to clipboard!", Toast.LENGTH_SHORT)
                                    .show();
                        } catch (JSONException e) {
                            Log.e(TAG, e.getMessage());
                            Toast.makeText(TapLockSettings.this, getString(R.string.msg_oops),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }).create();
    mDialog.show();
}

From source file:com.smc.tw.waltz.MainActivity.java

@Override
public void onAddDevice() {
    if (DEBUG)/*from  w w  w .  ja  va  2 s  . com*/
        Log.d(TAG, "onAddDevice");

    try {
        Intent intent = new Intent(getApplicationContext(), AddWaltzOneActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        intent.putExtra(AddWaltzOneActivity.FORCE_ADD, true);

        startActivity(intent);
    } catch (ActivityNotFoundException e) {
    }
}

From source file:com.radiusnetworks.scavengerhunt.ScavengerHuntApplication.java

private void sendNotification() {
    try {//from w  w w. jav  a 2s. c  om
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setContentTitle(getString(R.string.sh_notification_title))
                .setContentText(getString(R.string.sh_notification_text)).setVibrate(VIBRATOR_PATTERN)
                .setSmallIcon(R.drawable.sh_notification_icon).setAutoCancel(true);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        Intent notificationIntent = new Intent(this, TargetCollectionActivity.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        stackBuilder.addNextIntent(notificationIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:kr.me.ansr.gcmchat.gcm.MyGcmPushReceiver.java

/**
 * Showing notification with text only/*w  w  w  . ja v  a2 s. c  o  m*/
 * */
private void showNotificationMessage(Context context, String title, String message, String timeStamp,
        Intent intent) {
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    if (tab != null) {
        Log.d(TAG, "showNotificationMessage: " + tab);
        intent.putExtra("tab", tab);
    }
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}

From source file:examples.baku.io.permissions.examples.ComposeActivity.java

void initField(final PermissionedTextLayout edit, final String key) {
    edit.setSyncText(new SyncText(mDeviceId, PermissionManager.FLAG_SUGGEST, mSyncedMessageRef.child(key),
            mMessageRef.child(key)));//from   w ww . j  av  a 2s .  c om
    edit.setPermissionedTextListener(new PermissionedTextLayout.PermissionedTextListener() {
        @Override
        public void onSelected(final SyncTextDiff diff, PermissionedTextLayout text) {
            int current = mPermissionManager.getPermissions(mPath + "/" + key);
            if ((current & PermissionManager.FLAG_WRITE) == PermissionManager.FLAG_WRITE) {
                Intent content = new Intent(ComposeActivity.this, ComposeActivity.class);
                content.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                String name = diff.getSource();
                if (mPermissionService.getDiscovered().containsKey(name)) {
                    name = mPermissionService.getDiscovered().get(diff.source).getName();
                }
                mPermissionService.requestDialog(diff.source, "Apply changes from " + name, "be vigilant",
                        new PermissionService.ActionCallback() {
                            @Override
                            public boolean onAction(Intent intent) {
                                acceptSuggestions(diff.source);
                                return true;
                            }
                        }, new PermissionService.ActionCallback() {
                            @Override
                            public boolean onAction(Intent intent) {
                                rejectSuggestions(diff.source);
                                return true;
                            }
                        }, content);
            }
        }

        @Override
        public void onAction(int action, PermissionedTextLayout text) {
            if (action == 1) {
                if (mPublicBlessing != null) {
                    int current = mPublicBlessing.getPermissions(mPath + "/" + key);
                    if ((current & PermissionManager.FLAG_SUGGEST) == PermissionManager.FLAG_SUGGEST) {
                        mPublicBlessing.setPermissions(mPath + "/" + key,
                                current & ~PermissionManager.FLAG_SUGGEST);
                        text.setAction(1, new IconDrawable(ComposeActivity.this, MaterialIcons.md_cast)
                                .color(Color.BLACK).actionBarSize(), "Toggle Permission");
                    } else {
                        mPublicBlessing.setPermissions(mPath + "/" + key,
                                current | PermissionManager.FLAG_SUGGEST);
                        text.setAction(1, new IconDrawable(ComposeActivity.this, MaterialIcons.md_cancel)
                                .color(Color.RED).actionBarSize(), "Toggle Permission");
                    }
                }
            } else if (action == 2) {
                mPermissionManager.request(mPath + "/to", mDeviceId)
                        .setPermissions(PermissionManager.FLAG_WRITE).udpate();
            }
        }
    });

    mPermissionedFields.put(key, edit);
    final String path = "documents/" + mDeviceId + "/emails/messages/" + mId + "/" + key;

    mPermissionManager.addPermissionEventListener(mPath + "/" + key,
            new PermissionManager.OnPermissionChangeListener() {
                @Override
                public void onPermissionChange(int current) {
                    edit.onPermissionChange(current);

                    //TODO:generalize the following request button
                    if ("to".equals(key)) {
                        if (current == PermissionManager.FLAG_READ) {
                            edit.setAction(2, new IconDrawable(ComposeActivity.this, MaterialIcons.md_vpn_key),
                                    "Request Permission");
                        } else {
                            edit.removeAction(2);
                        }

                        if ((current & PermissionManager.FLAG_WRITE) == PermissionManager.FLAG_WRITE) {
                            edit.setAutoCompleteAdapter(contactAdapter);
                        } else {
                            edit.setAutoCompleteAdapter(null);
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
}

From source file:com.tencent.wstt.gt.activity.GTLogFragment.java

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.gtlog_search:
        Intent intent = new Intent(getActivity(), GTLogSearchActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);//  w w w . ja v a2 s  . co m
        break;
    case R.id.gtlog_delete:
        AlertDialog.Builder builder = new Builder(getActivity());
        builder.setMessage(getString(R.string.delete_tip));
        builder.setTitle(getString(R.string.delete));
        builder.setPositiveButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(getString(R.string.ok), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // do delete
                GTLogInternal.clearLog();
                // ??BHLog.clear()
                logAdapter.clear();
                // tagAdapter.clear(); ???tag
                dialog.dismiss();
            }
        });
        builder.show();
        break;
    case R.id.save_clean:
        et_savePath.setText("");
        break;
    case R.id.gtlog_save:
        String lastSaveLog = GTLogInternal.getLastSaveLog();
        if (lastSaveLog != null && lastSaveLog.contains(".") && lastSaveLog.endsWith(LogUtils.LOG_POSFIX)) {
            lastSaveLog = lastSaveLog.substring(0, lastSaveLog.lastIndexOf("."));
        }
        et_savePath.setText(lastSaveLog);
        dlg_save.show();
        break;
    case R.id.log_level:
        btn_level.setBackgroundResource(R.drawable.selected_Blue);
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                btn_level.setBackgroundResource(R.drawable.a_gt_log_btn_default_border);

            }
        }, 20);

        if (filterListView.getVisibility() == View.VISIBLE) {
            img_empty.setVisibility(View.GONE);
            filterListView.setVisibility(View.GONE);
        } else {
            filterListView.setAdapter(levelAdapter);
            filterListView.setVisibility(View.VISIBLE);
            img_empty.setVisibility(View.VISIBLE);
        }
        break;
    case R.id.log_tag:
        btn_tag.setBackgroundResource(R.drawable.selected_Blue);
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                btn_tag.setBackgroundResource(R.drawable.a_gt_log_btn_default_border);

            }
        }, 20);

        if (filterListView.getVisibility() == View.VISIBLE) {
            filterListView.setVisibility(View.GONE);
            img_empty.setVisibility(View.GONE);
        } else {
            final List<String> curTagList = new ArrayList<String>();
            curTagList.add("TAG");
            curTagList.addAll(GTLogInternal.getTags());
            tagAdapter = new ArrayAdapter<String>(getActivity(), R.layout.gt_simple_dropdown_item, curTagList);

            filterListView.setAdapter(tagAdapter);
            filterListView.setVisibility(View.VISIBLE);
            img_empty.setVisibility(View.VISIBLE);
        }
        break;
    case R.id.rl_loglist:
        if (rl_log_filter.getVisibility() == View.VISIBLE) {
            rl_log_filter.setVisibility(View.GONE);
            filterListView.setVisibility(View.GONE);
        } else {
            rl_log_filter.setVisibility(View.VISIBLE);
        }
        break;
    case R.id.log_msg:
        msgEtOnFocusOrClick();
        break;
    case R.id.log_msg_clear:
        et_Msg.setText("");
        GTLogInternal.setCurFilterMsg("");
        btn_msg_clear.setVisibility(View.GONE);
        // ((ArrayAdapter<?>)filterListView.getAdapter()).getFilter(
        // ).filter(BHLog.getCurFilterMsg());
        onLogChanged();
        break;
    case R.id.log_msg_cancel:
        cancelFilterMsgInput(v);
        break;
    case R.id.gtlog_open:
        showOpenLogDialog();
        break;
    }
}

From source file:kr.me.ansr.gcmchat.gcm.MyGcmPushReceiver.java

/**
 * Showing notification with text and image
 * *//* ww w . ja va 2s .  com*/
private void showNotificationMessageWithBigImage(Context context, String title, String message,
        String timeStamp, Intent intent, String imageUrl) {
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    if (tab != null) {
        Log.d(TAG, "showNotificationMessage: " + tab);
        intent.putExtra("tab", tab);
    }
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
}

From source file:dk.nota.lyt.libvlc.PlaybackService.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void showNotification() {
    PendingIntent piStop = PendingIntent.getBroadcast(this, REQ_CODE, new Intent(ACTION_REMOTE_STOP),
            PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent piBackward = PendingIntent.getBroadcast(this, REQ_CODE, new Intent(ACTION_REMOTE_BACKWARD),
            PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent piPlay = PendingIntent.getBroadcast(this, REQ_CODE, new Intent(ACTION_REMOTE_PLAYPAUSE),
            PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent piForward = PendingIntent.getBroadcast(this, REQ_CODE, new Intent(ACTION_REMOTE_FORWARD),
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder bob = new NotificationCompat.Builder(this);
    bob.addAction(R.drawable.ic_skip_previous_white_24dp, getText(R.string.previous), piBackward);
    if (mMediaPlayer.isPlaying()) {
        bob.addAction(R.drawable.ic_pause_white_24dp, getText(R.string.pause), piPlay);
    } else {//  w w w.  j  a  v a  2  s . c  om
        bob.addAction(R.drawable.ic_play_arrow_white_24dp, this.getText(R.string.play), piPlay);
    }
    bob.addAction(R.drawable.ic_skip_next_white_24dp, getText(R.string.next), piForward);

    boolean isPlaying = mMediaPlayer.isPlaying();
    long playbackPosition = mMediaPlayer.getTime();
    MediaWrapper currentMedia = getCurrentMedia();

    Log.d(TAG, "Playback position: " + playbackPosition);

    bob.setStyle(new NotificationCompat.MediaStyle().setMediaSession(mMediaSession.getSessionToken())
            .setShowActionsInCompactView(1).setShowCancelButton(true).setCancelButtonIntent(piStop))
            .setSmallIcon(R.drawable.ic_notification).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle(currentMedia.getTitle())
            .setContentText(currentMedia.getArtist() + " - " + currentMedia.getAlbum())
            .setTicker(currentMedia.getTitle()) //TODO: used by accessibility services!
            .setShowWhen(false).setDeleteIntent(piStop);

    if (mNotificationActivity != null) {
        Intent onClickIntent = new Intent(this, mNotificationActivity.getClass());
        onClickIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        onClickIntent.setAction(mNotificationAction);
        onClickIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent piOnClick = PendingIntent.getActivity(this, REQ_CODE, onClickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        bob.setContentIntent(piOnClick);
    }

    if (currentMedia.isPictureParsed()) {
        bob.setLargeIcon(currentMedia.getPicture());
    } else if (currentMedia.getArtworkURL() != null) {
        loadArtworkFromUrlAsync(currentMedia.getArtworkURL(), bob);
    } else {
        bob.setLargeIcon(getDefaultArtwork());
    }

    Notification notification = bob.build();
    if (!AndroidUtil.isLolliPopOrLater() || isPlaying) {
        startForeground(NOTIFICATION_ID, notification);
    } else {
        stopForeground(false);
        NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, notification);
    }
}

From source file:com.SecUpwN.AIMSICD.service.AimsicdService.java

/**
 * Set or update the Notification//  w  w w  . j a  v a 2s.c  o  m
 */
public void setNotification() {

    String tickerText;
    String contentText = "Phone Type " + mDevice.getPhoneType();

    String iconType = prefs.getString(this.getString(R.string.pref_ui_icons_key), "sense");

    int status;

    if (mFemtoDetected || mTypeZeroSmsDetected) {
        status = 4; //ALARM
    } else if (mChangedLAC) {
        status = 3; //MEDIUM
        contentText = "Hostile Service Area: Changing LAC Detected";
    } else if (mTrackingFemtocell || mTrackingCell || mLoaded) {
        status = 2; //NORMAL
        if (mTrackingFemtocell) {
            contentText = "FemtoCell Detection Active";
        } else if (mTrackingCell) {
            contentText = "Cell Tracking Active";
        }
    } else {
        status = 1; //IDLE
    }

    int icon = R.drawable.sense_idle;

    switch (status) {
    case 1: //IDLE
        switch (iconType) {
        case "flat":
            icon = R.drawable.flat_idle;
            break;
        case "sense":
            icon = R.drawable.sense_idle;
            break;
        case "white":
            icon = R.drawable.white_idle;
            break;
        }
        tickerText = getResources().getString(R.string.app_name_short) + " - Status: Idle";
        break;
    case 2: //NORMAL
        switch (iconType) {
        case "flat":
            icon = R.drawable.flat_ok;
            break;
        case "sense":
            icon = R.drawable.sense_ok;
            break;
        case "white":
            icon = R.drawable.white_ok;
            break;
        }
        tickerText = getResources().getString(R.string.app_name_short) + " - Status: Good No Threats Detected";
        break;
    case 3: //MEDIUM
        switch (iconType) {
        case "flat":
            icon = R.drawable.flat_medium;
            break;
        case "sense":
            icon = R.drawable.sense_medium;
            break;
        case "white":
            icon = R.drawable.white_medium;
            break;
        }
        tickerText = getResources().getString(R.string.app_name_short)
                + " - Hostile Service Area: Changing LAC Detected";
        break;
    case 4: //DANGER
        switch (iconType) {
        case "flat":
            icon = R.drawable.flat_danger;
            break;
        case "sense":
            icon = R.drawable.sense_danger;
            break;
        case "white":
            icon = R.drawable.white_danger;
            break;
        }
        tickerText = getResources().getString(R.string.app_name_short) + " - ALERT!! Threat Detected";
        if (mFemtoDetected) {
            contentText = "ALERT!! FemtoCell Connection Threat Detected";
        } else if (mTypeZeroSmsDetected) {
            contentText = "ALERT!! Type Zero Silent SMS Intercepted";
        }

        break;
    default:
        icon = R.drawable.sense_idle;
        tickerText = getResources().getString(R.string.app_name);
        break;
    }

    Intent notificationIntent = new Intent(mContext, AIMSICD.class);
    notificationIntent.putExtra("silent_sms", mTypeZeroSmsDetected);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_FROM_BACKGROUND);
    PendingIntent contentIntent = PendingIntent.getActivity(mContext, NOTIFICATION_ID, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Notification mBuilder = new NotificationCompat.Builder(this).setSmallIcon(icon).setTicker(tickerText)
            .setContentTitle(this.getResources().getString(R.string.app_name)).setContentText(contentText)
            .setOngoing(true).setAutoCancel(false).setContentIntent(contentIntent).build();

    NotificationManager mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder);
}