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.example.domiter.fileexplorer.fragment.SearchListFragment.java

private void browse(FileHolder file) {
    if (file.getFile().isDirectory()) {
        Intent intent = new Intent(getActivity(), FileManagerActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setData(Uri.parse(file.getFile().getAbsolutePath()));

        startActivity(intent);/*  w  w  w . j a  va 2s  .com*/
    } else {
        openFile(file, getActivity());
    }
}

From source file:com.cbtec.eliademy.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *///www  .  ja  v a2  s  . c  o  m

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, Eliademy.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.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

From source file:com.upnext.blekit.actions.BLEAction.java

/**
 * Displays a notification/*  w  ww  .  j  a v  a 2  s.c om*/
 *
 * @param context Android Context, passed from the calling entity
 * @param title title for the notification
 * @param msg message and ticker for the notification
 * @param type action type that will be put as an extra into the result Intent (<code>resultIntent.putExtra("type", type);</code>)
 * @param notificationIconResId notification icon resource id
 * @param notificationId an identifier for this notification as in {@link android.app.NotificationManager#notify(int, android.app.Notification)}
 */
public static void displayNotification(Context context, String title, String msg, String type,
        int notificationIconResId, int notificationId) {
    L.d(".");

    if (BLEKit.getTargetActivityForNotifications() == null) {
        throw new IllegalArgumentException(
                "Target activity for notifications is not set. Call BLEKit.getTargetActivityForNotifications() first.");
    }

    Notification.Builder builder = new Notification.Builder(context).setContentText(msg).setTicker(msg)
            .setContentTitle(title).setAutoCancel(true).setSmallIcon(notificationIconResId);

    Intent resultIntent = new Intent(context, BLEKit.getTargetActivityForNotifications());
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    resultIntent.putExtra("type", type);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notificationId, builder.build());
}

From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.services.SystemMonitor.java

private int systemNotice() {
    int t = START_STICKY;
    Log.e(SystemMonitor.class.getSimpleName(), "call me redundant BABY!  onStartCommand service");

    int myID = android.os.Process.myPid();
    // The intent to launch when the user clicks the expanded notification
    Intent intent = new Intent(this, MyHealthHubWithFragments.class);
    // Intent intent = new Intent();
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

    Notification notice = new Notification.Builder(getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
            .setWhen(System.currentTimeMillis()).setContentTitle(getPackageName())
            .setContentText("System Monitor running").setContentIntent(pendIntent).getNotification();

    notice.flags |= Notification.FLAG_AUTO_CANCEL;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(myID, notice);

    return t;/*from  w w  w .  jav a2s  .c o m*/
}

From source file:com.silentcircle.contacts.activities.ScContactDetailActivity.java

@Override
protected void onCreate(Bundle savedState) {
    super.onCreate(savedState);
    if (PhoneCapabilityTester.isUsingTwoPanes(this)) {
        // This activity must not be shown. We have to select the contact in the
        // PeopleActivity instead ==> Create a forward intent and finish
        final Intent originalIntent = getIntent();
        Intent intent = new Intent();
        intent.setAction(originalIntent.getAction());
        intent.setDataAndType(originalIntent.getData(), originalIntent.getType());

        // If we are launched from the outside, we should create a new task, because the user
        // can freely navigate the app (this is different from phones, where only the UP button
        // kicks the user into the full app)
        if (NavUtils.shouldUpRecreateTask(this, intent)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
                    | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
        } else {//  w ww  .j  av  a 2 s  . c o  m
            intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_FORWARD_RESULT
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }

        intent.setClass(this, ScContactsMainActivity.class);
        startActivity(intent);
        finish();
        return;
    }

    setContentView(R.layout.contact_detail_activity);

    mContactDetailLayoutController = new ContactDetailLayoutController(this, savedState,
            getSupportFragmentManager(), null, findViewById(R.id.contact_detail_container),
            mContactDetailFragmentListener);

    // We want the UP affordance but no app icon.
    // Setting HOME_AS_UP, SHOW_TITLE and clearing SHOW_HOME does the trick.
    ActionBar actionBar = this.getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE,
                ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
        actionBar.setTitle("");
    }

    Log.i(TAG, getIntent().getData().toString());
}

From source file:com.arifin.taxi.penumpang.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*  w  ww. j  ava2s.  c om*/
private void generateNotification(Context context, String message) {
    // System.out.println("this is message " + message);
    // System.out.println("NOTIFICATION RECEIVED!!!!!!!!!!!!!!" + message);
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, MainDrawerActivity.class);
    notificationIntent.putExtra("fromNotification", "notification");
    // 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,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    notification = builder.setContentIntent(intent).setSmallIcon(icon).setTicker(message).setWhen(when)
            .setAutoCancel(true).setContentTitle(title).setContentText(message).build();
    // mNM.notify(NOTIFICATION, notification);

    /*notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;*/

    System.out.println("notification====>" + message);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    // notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0x00000000;
    notification.ledOnMS = 0;
    notification.ledOffMS = 0;
    notificationManager.notify(0, notification);
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(
            PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
            "WakeLock");
    wakeLock.acquire();
    wakeLock.release();
}

From source file:com.craftsilicon.littlecabrider.GCMIntentService.java

private void generateNotificationNew(Context context, String message) {
    final Notification.Builder builder = new Notification.Builder(this);
    builder.setDefaults(/*  ww w  .j  av a 2s. c  o m*/
            Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
    builder.setStyle(new Notification.BigTextStyle(builder).bigText(message)
            .setBigContentTitle(context.getString(R.string.app_name)))
            .setContentTitle(context.getString(R.string.app_name)).setContentText(message)
            .setSmallIcon(R.drawable.ic_launcher);
    builder.setAutoCancel(true);
    Intent notificationIntent = new Intent(context, MainDrawerActivity.class);
    notificationIntent.putExtra("fromNotification", "notification");
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(intent);
    final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(0, builder.build());
}

From source file:io.github.marktony.espresso.service.ReminderService.java

/**
 * Set the details like title, subject, etc. of notifications.
 * @param position Position.//  w  ww  .  ja va 2s .co  m
 * @param pkg The package.
 * @return The notification.
 */
private Notification setNotifications(int position, Package pkg) {
    if (pkg != null) {

        Intent i = new Intent(getApplicationContext(), PackageDetailsActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        i.putExtra(PackageDetailsActivity.PACKAGE_ID, pkg.getNumber());

        PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), position, i,
                PendingIntent.FLAG_UPDATE_CURRENT);

        String title = pkg.getName();
        String subject;
        int smallIcon = R.drawable.ic_local_shipping_teal_24dp;
        if (Integer.parseInt(pkg.getState()) == Package.STATUS_DELIVERED) {
            subject = getString(R.string.delivered);
            smallIcon = R.drawable.ic_assignment_turned_in_teal_24dp;
        } else {
            if (Integer.parseInt(pkg.getState()) == Package.STATUS_ON_THE_WAY) {
                subject = getString(R.string.on_the_way);
            } else {
                subject = getString(R.string.notification_new_message);
            }
        }

        Notification notification = buildNotification(getApplicationContext(), title, subject,
                pkg.getData().get(0).getContext(), pkg.getData().get(0).getTime(), smallIcon,
                ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), intent, null);

        notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        notification.tickerText = title;

        return notification;
    }
    return null;
}

From source file:org.androidpn.client.Notifier.java

public void notify(String notificationId, String apiKey, String title, String message, String uri,
        String imageUrl) {// w  ww  .  j  a v a2s .c  om
    Log.d(LOGTAG, "notify()...");

    Log.d(LOGTAG, "notificationId=" + notificationId);
    Log.d(LOGTAG, "notificationApiKey=" + apiKey);
    Log.d(LOGTAG, "notificationTitle=" + title);
    Log.d(LOGTAG, "notificationMessage=" + message);
    Log.d(LOGTAG, "notificationUri=" + uri);

    if (isNotificationEnabled()) {
        // Show the toast
        if (isNotificationToastEnabled()) {
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        }
        mBuilder.setWhen(System.currentTimeMillis())//?
                .setPriority(Notification.PRIORITY_DEFAULT)//
                //            .setAutoCancel(true)//?????
                .setOngoing(false)//ture???,??(?)???,?(,??,)
                .setDefaults(Notification.DEFAULT_VIBRATE)//???????defaults??
                //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND  // requires VIBRATE permission
                .setSmallIcon(getNotificationIcon());

        mBuilder.setAutoCancel(true)//?
                .setContentTitle(title).setContentText(message).setTicker(message);
        // Notification
        if (isNotificationSoundEnabled()) {
            mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        }
        if (isNotificationVibrateEnabled()) {
            mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        }
        mBuilder.setOnlyAlertOnce(true);

        //            Intent intent;
        //            if (uri != null
        //                    && uri.length() > 0
        //                    && (uri.startsWith("http:") || uri.startsWith("https:")
        //                            || uri.startsWith("tel:") || uri.startsWith("geo:"))) {
        //                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        //            } else {
        //                String callbackActivityPackageName = sharedPrefs.getString(
        //                        Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, "");
        //                String callbackActivityClassName = sharedPrefs.getString(
        //                        Constants.CALLBACK_ACTIVITY_CLASS_NAME, "");
        //                intent = new Intent().setClassName(callbackActivityPackageName,
        //                        callbackActivityClassName);
        //                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        //            }

        Intent intent = new Intent(context, NotificationDetailsActivity.class);
        intent.putExtra(Constants.NOTIFICATION_ID, notificationId);
        intent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
        intent.putExtra(Constants.NOTIFICATION_TITLE, title);
        intent.putExtra(Constants.NOTIFICATION_MESSAGE, message);
        intent.putExtra(Constants.NOTIFICATION_URI, uri);
        intent.putExtra(Constants.NOTIFICATION_IMAGE_URL, imageUrl);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(context, random.nextInt(), intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(contentIntent);
        notificationManager.notify(random.nextInt(), mBuilder.build());

    } else {
        Log.w(LOGTAG, "Notificaitons disabled.");
    }
}

From source file:geofence.GeofenceTransitionsIntentService.java

/**
 * Posts a notification in the notification bar when a transition is detected.
 * If the user clicks the notification, control goes to the MapsActivity -> POIActivity.
 *//*from   ww  w. j  av a 2  s. c  o m*/
private void sendNotification(String notificationDetails) {
    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(getApplicationContext(), MapsActivity.class);
    notificationIntent.setAction(STARTPOI);
    notificationIntent.putExtra(MapsActivity.POINT, name);
    notificationIntent.putExtra(MainActivity.TOURNAME, tourName);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    // Get a PendingIntent containing the entire back stack.
    PendingIntent notificationPendingIntent =
            //                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent.getActivity(this, PendingIntent.FLAG_CANCEL_CURRENT, notificationIntent, 0);
    //                PendingIntent.getBroadcast(this, PendingIntent.FLAG_CANCEL_CURRENT, notificationIntent, 0);

    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.mipmap.ic_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setColor(Color.RED).setContentTitle(notificationDetails).setContentText("Notification")
            .setContentIntent(notificationPendingIntent);

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(TAG, 0, builder.build());
}