Example usage for android.content Intent setFlags

List of usage examples for android.content Intent setFlags

Introduction

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

Prototype

public @NonNull Intent setFlags(@Flags int flags) 

Source Link

Document

Set special flags controlling how this intent is handled.

Usage

From source file:com.frostwire.android.gui.util.UIUtils.java

public static void sendGoHomeIntent(Context ctx) {
    Intent i = new Intent(ctx, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("gohome-frostwire", true);
    ctx.startActivity(i);//w  ww . j  a  va  2s  .c  o m
}

From source file:com.bt.download.android.gui.util.UIUtils.java

/**
 * Opens the given file with the default Android activity for that File and
 * mime type./*www.ja  va2 s.  c o m*/
 * 
 * @param filePath
 * @param mime
 */
public static void openFile(Context context, String filePath, String mime) {
    try {
        if (!openAudioInternal(filePath)) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.fromFile(new File(filePath)), mime);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);

            if (mime != null && mime.contains("video")) {
                if (MusicUtils.isPlaying()) {
                    MusicUtils.playOrPause();
                }
                UXStats.instance().log(UXAction.LIBRARY_VIDEO_PLAY);
            }
        }
    } catch (Throwable e) {
        UIUtils.showShortMessage(context, R.string.cant_open_file);
        Log.e(TAG, "Failed to open file: " + filePath, e);
    }
}

From source file:at.diamonddogs.util.Utils.java

/**
 * Brings up the MAIN/LAUNCHER activity and clears the top
 *
 * @param context a {@link Context}//from w  w  w .j av  a2 s . com
 */
public static void returnToHome(Context context) {
    PackageManager pm = context.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setPackage(context.getPackageName());
    List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
    Intent homeIntent = new Intent("android.intent.action.MAIN");
    homeIntent.addCategory("android.intent.category.LAUNCHER");
    homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    homeIntent.setComponent(new ComponentName(context.getPackageName(), activities.get(0).activityInfo.name));
    context.startActivity(homeIntent);
}

From source file:com.carpool.dj.carpool.model.GcmIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*w w w.ja  va 2  s . c  o  m*/
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message, Bundle data, String type) {

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    // Notification notification = new Notification(icon, message, when);
    Notification notification = new Notification.Builder(context)
            .setContentTitle(Utils.nowActivity.getString(R.string.app_name)).setContentText(message)
            .setSmallIcon(icon).setWhen(when).setSound(alarmSound).build();

    // String title = context.getString(R.string.app_name);

    Intent notificationIntent = null;
    if ("CarEvent".equals(type)) {
        notificationIntent = new Intent(context, ContentActivity.class);
    } else {
        return;
    }
    notificationIntent.putExtras(data);
    // 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);
    notification.setLatestEventInfo(context, Utils.nowActivity.getString(R.string.app_name), message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, notification);

}

From source file:com.facebook.notifications.NotificationsManager.java

/**
 * Present a {@link Notification} to be presented from a GCM push bundle.
 * <p/>// ww w .  jav  a 2s.  co  m
 * This does not present a notification immediately, instead it caches the assets from the
 * notification bundle, and then presents a notification to the user. This allows for a smoother
 * interaction without loading indicators for the user.
 * <p/>
 * Note that only one notification can be created for a specific push bundle, should you attempt
 * to present a new notification with the same payload bundle as an existing notification, it will
 * replace and update the old notification.
 *
 * @param context              The context to send the notification from
 * @param notificationBundle   The content of the push notification
 * @param launcherIntent       The launcher intent that contains your Application's activity.
 *                             This will be modified with the FLAG_ACTIVITY_CLEAR_TOP and
 *                             FLAG_ACTIVITY_SINGLE_TOP flags, in order to properly show the
 *                             notification in an already running application.
 *                             <p/>
 *                             Should you not want this behavior, you may use the notificationExtender
 *                             parameter to customize the contentIntent of the notification before
 *                             presenting it.
 * @param notificationExtender A nullable argument that allows you to customize the notification
 *                             before displaying it. Use this to configure Icons, text, sounds,
 *                             etc. before we pass the notification off to the OS.
 */
public static boolean presentNotification(@NonNull final Context context,
        @NonNull final Bundle notificationBundle, @NonNull final Intent launcherIntent,
        @Nullable final NotificationExtender notificationExtender) {
    final JSONObject alert;
    final int payloadHash;

    try {
        String payload = notificationBundle.getString(CARD_PAYLOAD_KEY);
        if (payload == null) {
            return false;
        }
        payloadHash = payload.hashCode();

        JSONObject payloadObject = new JSONObject(payload);
        alert = payloadObject.optJSONObject("alert") != null ? payloadObject.optJSONObject("alert")
                : new JSONObject();
    } catch (JSONException ex) {
        Log.e(LOG_TAG, "Error while parsing notification bundle JSON", ex);
        return false;
    }

    final boolean[] success = new boolean[1];

    final Thread backgroundThread = new Thread(new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            prepareCard(context, notificationBundle, new PrepareCallback() {
                @Override
                public void onPrepared(@NonNull Intent presentationIntent) {
                    Intent contentIntent = new Intent(launcherIntent);
                    contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    contentIntent.putExtra(EXTRA_PAYLOAD_INTENT, presentationIntent);

                    NotificationManager manager = (NotificationManager) context
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                    Notification.Builder builder = new Notification.Builder(context)
                            .setSmallIcon(android.R.drawable.ic_dialog_alert)
                            .setContentTitle(alert.optString("title")).setContentText(alert.optString("body"))
                            .setAutoCancel(true)
                            .setContentIntent(PendingIntent.getActivity(context.getApplicationContext(),
                                    payloadHash, contentIntent, PendingIntent.FLAG_ONE_SHOT));

                    if (notificationExtender != null) {
                        builder = notificationExtender.extendNotification(builder);
                    }

                    manager.notify(NOTIFICATION_TAG, payloadHash, builder.getNotification());
                    success[0] = true;
                    Looper.myLooper().quit();
                }

                @Override
                public void onError(@NonNull Exception exception) {
                    Log.e(LOG_TAG, "Error while preparing card", exception);
                    Looper.myLooper().quit();
                }
            });

            Looper.loop();
        }
    });

    backgroundThread.start();

    try {
        backgroundThread.join();
    } catch (InterruptedException ex) {
        Log.e(LOG_TAG, "Failed to wait for background thread", ex);
        return false;
    }
    return success[0];
}

From source file:com.example.mynsocial.BluetoothChat.java

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    Log.i(TAG, "---------------------------------------");
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent,
            0);// w  w w  .ja  v a  2 s  . co  m
    String[][] techList = new String[][] {};
    adapter.enableForegroundDispatch(activity, pendingIntent, null, techList);
}

From source file:com.frostwire.android.gui.util.UIUtils.java

/**
 * Opens the given file with the default Android activity for that File and
 * mime type.//from ww  w  . java 2  s.com
 */
public static void openFile(Context context, String filePath, String mime, boolean useFileProvider) {
    try {
        if (filePath != null && !openAudioInternal(context, filePath)) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(getFileUri(context, filePath, useFileProvider), Intent.normalizeMimeType(mime));
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (mime != null && mime.contains("video")) {
                if (MusicUtils.isPlaying()) {
                    MusicUtils.playOrPause();
                }
                UXStats.instance().log(UXAction.LIBRARY_VIDEO_PLAY);
            }
            context.startActivity(i);
        }
    } catch (Throwable e) {
        UIUtils.showShortMessage(context, R.string.cant_open_file);
        LOG.error("Failed to open file: " + filePath, e);
    }
}

From source file:net.frygo.findmybuddy.GCMIntentService.java

private static void generateNotification(Context context, String message) {

    Random rand = new Random();
    int x = rand.nextInt();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, customlistview.class);
    notificationIntent.putExtra("alert", message);
    message = message + " would like to add you as friend";
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification(R.drawable.logo, message, System.currentTimeMillis());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(x, notification);

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock mWakelock = pm
            .newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, title);
    mWakelock.acquire();/*  w ww. ja va 2  s  .c  o m*/

    // Timer before putting Android Device to sleep mode.
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            mWakelock.release();
        }
    };
    timer.schedule(task, 5000);

}

From source file:net.frygo.findmybuddy.GCMIntentService.java

private static void generateAcceptfriendNotification(Context context, String message, String status) {

    Random rand = new Random();
    int x = rand.nextInt();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, customlistview.class);
    if (status.equalsIgnoreCase("accept"))
        message = message + " added you as buddy";
    else/*from  w  ww. j  a v a 2s.  co m*/
        message = message + " rejected you as buddy";
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification(R.drawable.logo, message, System.currentTimeMillis());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(x, notification);

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock mWakelock = pm
            .newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, title);
    mWakelock.acquire();

    // Timer before putting Android Device to sleep mode.
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            mWakelock.release();
        }
    };
    timer.schedule(task, 5000);

}

From source file:com.chen.download.Utils.java

/**
 * ?/* w w w. ja  v a  2 s . c om*/
 * 
 * @param context
 *            
 * @param pkgName
 *            ??
 */
public static void uninstallApk(Context context, String pkgName) {
    Uri packageURI = Uri.parse("package:" + pkgName);
    Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
    uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(uninstallIntent);
}