Example usage for android.app TaskStackBuilder create

List of usage examples for android.app TaskStackBuilder create

Introduction

In this page you can find the example usage for android.app TaskStackBuilder create.

Prototype

public static TaskStackBuilder create(Context context) 

Source Link

Document

Return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities.

Usage

From source file:com.lambdasoup.quickfit.alarm.AlarmService.java

/**
 * Replaces the notification for alarms with a notification about a single workout.
 * <p>//w w w  . j a v  a  2  s  .  c o  m
 * Relies on the caller to position the cursor on the desired row and to close the cursor.
 *
 * @param cursor       Cursor to read the workout data from
 * @param cancelIntent Pending intent to pass on to the content intent, allowing its receiver to
 *                     execute it (update notification state in db to the fact that the notification
 *                     is now cancelled)
 */
@WorkerThread
private @NonNull NotificationCompat.Builder notifySingleEvent(@NonNull Cursor cursor,
        PendingIntent cancelIntent) {
    NotificationCompat.Builder notification = new NotificationCompat.Builder(this);

    FitActivity fitActivity = FitActivity
            .fromKey(cursor.getString(cursor.getColumnIndex(WorkoutEntry.ACTIVITY_TYPE)), getResources());
    String label = "";
    if (!cursor.isNull(cursor.getColumnIndex(WorkoutEntry.LABEL))) {
        label = cursor.getString(cursor.getColumnIndex(WorkoutEntry.LABEL));
    }
    int durationMinutes = cursor.getInt(cursor.getColumnIndex(WorkoutEntry.DURATION_MINUTES));

    String title = getString(R.string.notification_alarm_title_single, fitActivity.displayName);
    String formattedMinutes = String.format(
            getResources().getQuantityString(R.plurals.duration_mins_format, durationMinutes), durationMinutes);
    String content = getString(R.string.notification_alarm_content_single, formattedMinutes, label);

    notification.setContentTitle(title);
    notification.setContentText(content);

    long workoutId = cursor.getLong(cursor.getColumnIndex(WorkoutEntry.WORKOUT_ID));

    Intent workoutIntent = new Intent(getApplicationContext(), WorkoutListActivity.class);
    workoutIntent.putExtra(WorkoutListActivity.EXTRA_SHOW_WORKOUT_ID, workoutId);
    workoutIntent.putExtra(WorkoutListActivity.EXTRA_NOTIFICATIONS_CANCEL_INTENT, cancelIntent);
    PendingIntent activityIntent = TaskStackBuilder.create(this).addNextIntentWithParentStack(workoutIntent)
            .getPendingIntent(Constants.PENDING_INTENT_WORKOUT_LIST, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setContentIntent(activityIntent);// open workout list, scroll to workout

    long scheduleId = cursor.getLong(cursor.getColumnIndex(WorkoutEntry.SCHEDULE_ID));

    PendingIntent didItIntent = PendingIntent.getService(getApplicationContext(),
            Constants.PENDING_INTENT_DID_IT, getIntentOnDidIt(getApplicationContext(), scheduleId, workoutId),
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(R.drawable.ic_done_white_24dp, getString(R.string.notification_action_did_it),
            didItIntent);

    PendingIntent snoozeIntent = PendingIntent.getService(getApplicationContext(),
            Constants.PENDING_INTENT_SNOOZE, AlarmService.getIntentOnSnooze(this, scheduleId),
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.addAction(R.drawable.ic_alarm_white_24dp,
            getString(R.string.notification_action_remind_me_later), snoozeIntent);

    return notification;
}

From source file:net.wespot.pim.view.InqCommunicateFragment.java

private void createNotification(MessageLocalObject messageLocalObject, Long runId) {

    if (!runIdList.contains(runId)) {
        runIdList.add(runId);// www .  ja v a  2  s.c om
        runIdList_str.add(runId.toString());
    }

    numMessages = 0;
    mBuilder = null;
    mNotificationStyle = null;
    mNotificationManager = null;

    mNotificationManager = (NotificationManager) ARL.getContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationStyle = new NotificationCompat.InboxStyle();
    mBuilder = new NotificationCompat.Builder(ARL.getContext()).setSmallIcon(R.drawable.ic_launcher)
            .setAutoCancel(true).setSortKey("0")
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
            .setStyle(mNotificationStyle);

    InquiryLocalObject inquiryLocalObject = DaoConfiguration.getInstance().getInquiryLocalObjectDao()
            .queryBuilder().where(InquiryLocalObjectDao.Properties.RunId.eq(runId)).list().get(0);

    // 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(ARL.getContext());

    Intent resultIntent;

    if (runIdList.size() > 1) {
        resultIntent = new Intent(ARL.getContext(), PimInquiriesFragment.class);
        resultIntent.putStringArrayListExtra(INQUIRIES_ID, (ArrayList<String>) runIdList_str);

        Intent parent = new Intent(ARL.getContext(), MainActivity.class);
        Intent parent1 = new Intent(ARL.getContext(), SplashActivity.class);

        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addNextIntentWithParentStack(parent1);
        stackBuilder.addNextIntentWithParentStack(parent);

    } else {
        resultIntent = new Intent(ARL.getContext(), InquiryPhasesActivity.class);
        resultIntent.putExtra(INQUIRY_ID, inquiryLocalObject.getId());

        Intent parent = new Intent(ARL.getContext(), PimInquiriesFragment.class);
        Intent parent1 = new Intent(ARL.getContext(), MainActivity.class);
        Intent parent2 = new Intent(ARL.getContext(), SplashActivity.class);

        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addNextIntentWithParentStack(parent2);
        stackBuilder.addNextIntentWithParentStack(parent1);
        stackBuilder.addNextIntentWithParentStack(parent);
    }

    // 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);

    if (runIdList.size() == 1) {
        // More than 1 inquiries have messages

        mBuilder.setContentTitle(inquiryLocalObject.getTitle());
        for (MessageLocalObject me : notications_queue_messages) {
            mNotificationStyle.addLine(getNameUser(me.getAuthor()) + ": " + me.getBody()).setSummaryText(
                    ++numMessages != 1 ? numMessages + " new messages" : numMessages + " new message");
        }
    }

    if (runIdList.size() > 1) {
        // More than 1 inquiries have messages

        for (MessageLocalObject me : notications_queue_messages) {
            mBuilder.setContentTitle("Personal Inquiry Manager");
            InquiryLocalObject a = DaoConfiguration.getInstance().getInquiryLocalObjectDao().queryBuilder()
                    .where(InquiryLocalObjectDao.Properties.RunId.eq(me.getRunId())).list().get(0);

            mNotificationStyle.addLine(getNameUser(me.getAuthor()) + " @ " + a.getTitle() + ": " + me.getBody())
                    .setSummaryText(++numMessages != 1
                            ? numMessages + " new messages from " + runIdList.size() + " conversations" + " "
                            : numMessages + " new message from " + runIdList.size() + " conversations");
        }
    }

    mNotificationManager.notify(Integer.parseInt(NUMBER), mBuilder.build());
}

From source file:com.lambdasoup.quickfit.alarm.AlarmService.java

/**
 * Replaces the notification for alarms with a notification about multiple workouts.
 * <p>/* ww  w  .  j  a v  a2 s  .  co  m*/
 * Relies on the caller to position the cursor before the first row and to close the cursor.
 *
 * @param cursor       Cursor to read the workout data from
 * @param cancelIntent Pending intent to pass on to the content intent, allowing its receiver to
 *                     execute it (update notification state in db to the fact that the notification
 *                     is now cancelled)
 */
@WorkerThread
private @NonNull NotificationCompat.Builder notifyMultipleEvents(@NonNull Cursor cursor,
        PendingIntent cancelIntent) {
    NotificationCompat.Builder notification = new NotificationCompat.Builder(this);

    Intent workoutIntent = new Intent(getApplicationContext(), WorkoutListActivity.class);
    workoutIntent.putExtra(WorkoutListActivity.EXTRA_NOTIFICATIONS_CANCEL_INTENT, cancelIntent);
    PendingIntent activityIntent = TaskStackBuilder.create(this).addNextIntentWithParentStack(workoutIntent)
            .getPendingIntent(Constants.PENDING_INTENT_WORKOUT_LIST, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setContentIntent(activityIntent);

    InboxStyle inboxStyle = new InboxStyle();
    while (cursor.moveToNext()) {
        FitActivity fitActivity = FitActivity
                .fromKey(cursor.getString(cursor.getColumnIndex(WorkoutEntry.ACTIVITY_TYPE)), getResources());
        String label = "";
        if (!cursor.isNull(cursor.getColumnIndex(WorkoutEntry.LABEL))) {
            label = cursor.getString(cursor.getColumnIndex(WorkoutEntry.LABEL));
        }
        int durationMinutes = cursor.getInt(cursor.getColumnIndex(WorkoutEntry.DURATION_MINUTES));

        String formattedMinutes = String.format(
                getResources().getQuantityString(R.plurals.duration_mins_format, durationMinutes),
                durationMinutes);
        String line = getString(R.string.notification_alarm_content_line_multi, fitActivity.displayName,
                formattedMinutes, label);
        inboxStyle.addLine(line);
    }

    notification.setContentTitle(getString(R.string.notification_alarm_title_multi));
    notification
            .setContentText(getString(R.string.notification_alarm_content_summary_multi, cursor.getCount()));

    notification.setStyle(inboxStyle);

    return notification;
}

From source file:com.ravi.apps.android.newsbytes.sync.NewsSyncAdapter.java

/**
 * Notifies the user that fresh news updates are now available.
 *//*from w  w w.  ja  v  a  2  s .  c  om*/
private void sendNewsNotification(Context context) {
    // Create the intent to open the app when the user taps on the notification.
    Intent mainIntent = new Intent(context, MainActivity.class);

    // Create the stack builder object.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

    // Add the intent to the top of the stack.
    stackBuilder.addNextIntent(mainIntent);

    // Get a pending intent containing the entire back stack.
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Create the notification builder object.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    // Set the pending intent onto the notification.
    builder.setContentIntent(pendingIntent);

    // Set the notification to automatically dismiss after the user taps it.
    builder.setAutoCancel(true);

    // Set the notification title and text.
    builder.setContentTitle(context.getString(R.string.app_name))
            .setContentText(context.getString(R.string.msg_notification_text));

    // Set the notification ticker.
    builder.setTicker(context.getString(R.string.msg_notification_text));

    // Set the notification large and small icons.
    builder.setSmallIcon(R.drawable.ic_notify_small)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_notify_large));

    // Send the notification.
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);
    notificationManager.notify(NEWS_NOTIFICATION_ID, builder.build());
}

From source file:de.bahnhoefe.deutschlands.bahnhofsfotos.DetailsActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.nav_to_station:
        startNavigation(DetailsActivity.this);
        break;/*from   www .ja va2s. c  o  m*/
    case R.id.timetable:
        final Intent timetableIntent = new Timetable().createTimetableIntent(country, bahnhof);
        if (timetableIntent != null) {
            startActivity(timetableIntent);
        } else {
            Toast.makeText(this, R.string.timetable_missing, Toast.LENGTH_LONG).show();
        }
        break;
    case R.id.photo_upload:
        if (isMyDataIncomplete()) {
            checkMyData();
        } else {
            if (TextUtils.isEmpty(email) || TextUtils.isEmpty(token)) {
                Toast.makeText(this, R.string.registration_needed, Toast.LENGTH_LONG).show();
            } else {
                uploadPhoto();
            }
        }
        break;
    case R.id.share_photo:
        Intent shareIntent = createFotoSendIntent();
        shareIntent.putExtra(Intent.EXTRA_TEXT, country.getTwitterTags() + " " + bahnhof.getTitle());
        shareIntent.setType("image/jpeg");
        startActivity(createChooser(shareIntent, "send"));
        break;
    case android.R.id.home:
        Intent upIntent = NavUtils.getParentActivityIntent(this);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is NOT part of this app's task, so create a new task
            // when navigating up, with a synthesized back stack.
            TaskStackBuilder.create(this)
                    // Add all of this activity's parents to the back stack
                    .addNextIntentWithParentStack(upIntent)
                    // Navigate up to the closest parent
                    .startActivities();
        } else {
            onBackPressed();
        }

        break;
    default:
        return super.onOptionsItemSelected(item);
    }

    return true;
}

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

private void sendNotification() {
    try {/*from   w  w w.j a  v a2s . c o  m*/
        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:re.serialout.MainScreen.java

public void buildNotification(Context context) {
    //Feature in progress, not in current version.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle("There is a new survey for you to complete!");
    builder.setContentText("Please take a second to fill it out.");
    builder.setSmallIcon(R.drawable.logo2);
    builder.setAutoCancel(true);//w  ww  . j  a v  a  2  s . c o m
    System.out.println("BUILT");
    Intent resultIntent = new Intent(this, MainScreen.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainScreen.class);
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(100, builder.build());
}

From source file:br.com.Utilitarios.WorkUpService.java

public void criarNotificacoes(Notificacoes n, String titulo, String texto, String ticker, String tipo,
        String extra) {/*from  www. j a va2  s . c  om*/

    // Build notification
    NotificationCompat.Builder noti = new NotificationCompat.Builder(this);
    noti.setContentTitle(titulo);
    noti.setContentText(texto);
    noti.setTicker(ticker);
    noti.setSmallIcon(R.drawable.ic_launcher);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    noti.setSound(alarmSound);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Intent resultIntent = null;
    if (tipo.equals("novoPersonal")) {
        // Creates an expnovaAvaliacaolicit intent for an Activity in your app
        resultIntent = new Intent(this, AceitarRejeitarAmigo.class);
        resultIntent.putExtra("usuario", n.getOrigemNotificacao());
        resultIntent.putExtra("tipo", "aluno");
    }

    if (tipo.equals("novoAluno")) {

        resultIntent = new Intent(this, AceitarRejeitarAmigo.class);
        resultIntent.putExtra("usuario", n.getOrigemNotificacao());
        resultIntent.putExtra("tipo", "personal");

    }

    if (tipo.equals("novaAula")) {
        // Creates an explicit intent for an Activity in your app
        resultIntent = new Intent(this, ConfirmarAula.class);
        resultIntent.putExtra("codAula", extra);
    }

    //This ensures that navigating backward from the Activity leads out of the app to Home page
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    if (tipo.equals("novoPersonal") || tipo.equals("novoAluno")) {
        // Adds the back stack for the Intent
        stackBuilder.addParentStack(AceitarRejeitarAmigo.class);

        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT //can only be used once
        );
        // start the activity when the user clicks the notification text
        noti.setContentIntent(resultPendingIntent);
    }

    // pass the Notification object to the system
    notificationManager.notify(0, noti.build());

    n.visualizarNotificacao(n.getCodNotificacao());

    //----------------------------------------------------------------------------------
}

From source file:org.tigase.messenger.phone.pro.service.XMPPService.java

private void processAuthenticationError(final Jaxmpp jaxmpp) {
    Log.e(TAG, "Invalid credentials of account " + jaxmpp.getSessionObject().getUserBareJid());
    jaxmpp.getSessionObject().setUserProperty("CC:DISABLED", true);

    String title = getString(R.string.notification_credentials_error_title,
            jaxmpp.getSessionObject().getUserBareJid().toString());
    String text = getString(R.string.notification_certificate_error_text);

    Intent resultIntent = new Intent(this, LoginActivity.class);
    resultIntent.putExtra("account_name", jaxmpp.getSessionObject().getUserBareJid().toString());

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ChatActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent editServerSettingsPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            // .setSmallIcon(R.drawable.ic_messenger_icon)
            .setSmallIcon(android.R.drawable.stat_notify_error).setWhen(System.currentTimeMillis())
            .setAutoCancel(true).setTicker(title).setContentTitle(title).setContentText(text)
            .setContentIntent(editServerSettingsPendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    builder.setLights(0xffff0000, 100, 100);

    // getNotificationManager().notify(notificationId, builder.build());

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(("error:" + jaxmpp.getSessionObject().getUserBareJid().toString()).hashCode(),
            builder.build());/*from  w w  w. j  ava2  s  . c o  m*/
}

From source file:org.tigase.messenger.phone.pro.service.XMPPService.java

private void processCertificateError(final Jaxmpp jaxmpp,
        final SecureTrustManagerFactory.DataCertificateException cause) {
    Log.e(TAG, "Invalid certificate of account " + jaxmpp.getSessionObject().getUserBareJid() + ": "
            + cause.getMessage());/*from  w ww .  ja  va2s. c  om*/
    jaxmpp.getSessionObject().setUserProperty("CC:DISABLED", true);

    String title = getString(R.string.notification_certificate_error_title,
            jaxmpp.getSessionObject().getUserBareJid().toString());
    String text = getString(R.string.notification_certificate_error_text);

    Intent resultIntent = new Intent(this, LoginActivity.class);
    resultIntent.putExtra("account_name", jaxmpp.getSessionObject().getUserBareJid().toString());

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ChatActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent editServerSettingsPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            // .setSmallIcon(R.drawable.ic_messenger_icon)
            .setSmallIcon(android.R.drawable.stat_notify_error).setWhen(System.currentTimeMillis())
            .setAutoCancel(true).setTicker(title).setContentTitle(title).setContentText(text)
            .setContentIntent(editServerSettingsPendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    builder.setLights(0xffff0000, 100, 100);

    // getNotificationManager().notify(notificationId, builder.build());

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(("error:" + jaxmpp.getSessionObject().getUserBareJid().toString()).hashCode(),
            builder.build());
}