Java Code Examples for android.support.v4.app.TaskStackBuilder#addNextIntentWithParentStack()

The following examples show how to use android.support.v4.app.TaskStackBuilder#addNextIntentWithParentStack() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SteemActionsNotificationHandler.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
public static PendingIntent getPostNotificationPendingIntent(Context context,
                                                             String notificationId,
                                                             String author,
                                                             String parentPermlink,
                                                             String permlink) {
  Intent intent = new Intent(context, DetailedActivity.class);
  Bundle bundle = new Bundle();
  bundle.putString(Constants.EXTRAA_KEY_NOTIFICATION_ID, notificationId);
  bundle.putString(Constants.EXTRAA_KEY_POST_AUTHOR, author);
  bundle.putString(Constants.EXTRAA_KEY_PARENT_PERMLINK, parentPermlink);
  bundle.putString(Constants.EXTRAA_KEY_POST_PERMLINK, permlink);
  intent.putExtras(bundle);
  TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  stackBuilder.addNextIntentWithParentStack(intent);
  return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
 
Example 2
Source File: CompetitionNotificationHandler.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
/**
 * @param context Application context.
 * @return Pending Intent for competition.
 */
private static PendingIntent getCompetitionWinnersListPendingIntent(Context context, String compId, String compTitle) {
  Intent intent = new Intent(context, WinnersFeedListActivity.class);
  Bundle bundle = new Bundle();
  intent.putExtras(bundle);
  intent.putExtra(EXTRA_COMPETITION_ID, compId);
  intent.putExtra(EXTRA_COMPETITION_TITLE, compTitle);
  TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  stackBuilder.addNextIntentWithParentStack(intent);
  return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
 
Example 3
Source File: CharacterActivity.java    From Villains-and-Heroes with Apache License 2.0 5 votes vote down vote up
public static PendingIntent getPendingIntent(@NonNull Context context, @NonNull CharacterVO character, int id) {

        Intent intent = new Intent(context, CharacterActivity.class);
        intent.setAction(Integer.toString(id)); // Used to update all PendingIntent extras data for each widget
        intent.putExtra(EXTRA_CHARACTER, character);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntentWithParentStack(intent); // Return to MainActivity

        return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    }
 
Example 4
Source File: BaseActivity.java    From privacy-friendly-pedometer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enables back navigation for activities that are launched from the NavBar. See
 * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
 *
 * @param intent
 */
private void createBackStack(Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        TaskStackBuilder builder = TaskStackBuilder.create(this);
        builder.addNextIntentWithParentStack(intent);
        builder.startActivities();
    } else {
        startActivity(intent);
        finish();
    }
}
 
Example 5
Source File: BaseActivity.java    From privacy-friendly-torchlight with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enables back navigation for activities that are launched from the NavBar. See
 * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
 * @param intent
 */
private void createBackStack(Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        TaskStackBuilder builder = TaskStackBuilder.create(this);
        builder.addNextIntentWithParentStack(intent);
        builder.startActivities();
    } else {
        startActivity(intent);
        finish();
    }
}
 
Example 6
Source File: BaseActivity.java    From privacy-friendly-weather with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enables back navigation for activities that are launched from the NavBar. See
 * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
 * @param intent
 */
private void createBackStack(Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        TaskStackBuilder builder = TaskStackBuilder.create(this);
        builder.addNextIntentWithParentStack(intent);
        builder.startActivities();
    } else {
        startActivity(intent);
        finish();
    }
}
 
Example 7
Source File: BaseActivity.java    From privacy-friendly-shopping-list with Apache License 2.0 5 votes vote down vote up
/**
 * Enables back navigation for activities that are launched from the NavBar. See
 * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
 *
 * @param intent
 */
private void createBackStack(Intent intent)
{
    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN )
    {
        TaskStackBuilder builder = TaskStackBuilder.create(this);
        builder.addNextIntentWithParentStack(intent);
        builder.startActivities();
    }
    else
    {
        startActivity(intent);
        finish();
    }
}
 
Example 8
Source File: BaseActivity.java    From privacy-friendly-dame with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enables back navigation for activities that are launched from the NavBar. See
 * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
 * @param intent
 */
private void createBackStack(Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        TaskStackBuilder builder = TaskStackBuilder.create(this);
        builder.addNextIntentWithParentStack(intent);
        builder.startActivities();
    } else {
        startActivity(intent);
        finish();
    }
}
 
Example 9
Source File: SteemActionsNotificationHandler.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
public static PendingIntent getTransferPendingIntent(Context context, String notificationId) {
  Intent intent = new Intent(context, AccountHistoryActivity.class);
  Bundle bundle = new Bundle();
  String username = HaprampPreferenceManager.getInstance().getCurrentSteemUsername();
  bundle.putString(EXTRA_USERNAME, username);
  bundle.putString(Constants.EXTRAA_KEY_NOTIFICATION_ID, notificationId);
  intent.putExtras(bundle);
  TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  stackBuilder.addNextIntentWithParentStack(intent);
  return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
 
Example 10
Source File: BaseActivity.java    From privacy-friendly-pin-mnemonic with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enables back navigation for activities that are launched from the NavBar. See
 * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
 *
 * @param intent
 */
private void createBackStack(Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        TaskStackBuilder builder = TaskStackBuilder.create(this);
        builder.addNextIntentWithParentStack(intent);
        builder.startActivities();
    } else {
        startActivity(intent);
        finish();
    }
}
 
Example 11
Source File: CompetitionNotificationHandler.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
/**
 * @param context Application context.
 * @return Pending Intent for competition.
 */
public static PendingIntent getCompetitionParticipatePendingIntent(Context context, String hashtag, String id, String title) {
  Intent intent = new Intent(context, ParticipateEditorActivity.class);
  intent.putExtra(EXTRA_COMPETITION_HASHTAG, hashtag);
  intent.putExtra(EXTRA_COMPETITION_ID, id);
  intent.putExtra(EXTRA_COMPETITION_TITLE, title);
  TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  stackBuilder.addNextIntentWithParentStack(intent);
  return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
 
Example 12
Source File: CompetitionNotificationHandler.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
/**
 * @param context Application context.
 * @return Pending Intent for competition.
 */
public static PendingIntent getCompetitionListingPendingIntent(Context context) {
  Intent intent = new Intent(context, HomeActivity.class);
  Bundle bundle = new Bundle();
  intent.putExtras(bundle);
  intent.putExtra(EXTRA_TAB_INDEX, 1);
  TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  stackBuilder.addNextIntentWithParentStack(intent);
  return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
 
Example 13
Source File: BaseActivity.java    From privacy-friendly-passwordgenerator with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enables back navigation for activities that are launched from the NavBar. See
 * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
 * @param intent
 */
private void createBackStack(Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        TaskStackBuilder builder = TaskStackBuilder.create(this);
        builder.addNextIntentWithParentStack(intent);
        builder.startActivities();
    } else {
        startActivity(intent);
        finish();
    }
}
 
Example 14
Source File: NotificationUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
     * Constructs and displays a notification for the newly updated weather for today.
     *
     * @param context Context used to query our ContentProvider and use various Utility methods
     */
    public static void notifyUserOfNewWeather(Context context) {

        /* Build the URI for today's weather in order to show up to date data in notification */
        Uri todaysWeatherUri = WeatherContract.WeatherEntry
                .buildWeatherUriWithDate(SunshineDateUtils.normalizeDate(System.currentTimeMillis()));

        /*
         * The MAIN_FORECAST_PROJECTION array passed in as the second parameter is defined in our WeatherContract
         * class and is used to limit the columns returned in our cursor.
         */
        Cursor todayWeatherCursor = context.getContentResolver().query(
                todaysWeatherUri,
                WEATHER_NOTIFICATION_PROJECTION,
                null,
                null,
                null);

        /*
         * If todayWeatherCursor is empty, moveToFirst will return false. If our cursor is not
         * empty, we want to show the notification.
         */
        if (todayWeatherCursor.moveToFirst()) {

            /* Weather ID as returned by API, used to identify the icon to be used */
            int weatherId = todayWeatherCursor.getInt(INDEX_WEATHER_ID);
            double high = todayWeatherCursor.getDouble(INDEX_MAX_TEMP);
            double low = todayWeatherCursor.getDouble(INDEX_MIN_TEMP);

            Resources resources = context.getResources();
            int largeArtResourceId = SunshineWeatherUtils
                    .getLargeArtResourceIdForWeatherCondition(weatherId);

            Bitmap largeIcon = BitmapFactory.decodeResource(
                    resources,
                    largeArtResourceId);

            String notificationTitle = context.getString(R.string.app_name);

            String notificationText = getNotificationText(context, weatherId, high, low);

            /* getSmallArtResourceIdForWeatherCondition returns the proper art to show given an ID */
            int smallArtResourceId = SunshineWeatherUtils
                    .getSmallArtResourceIdForWeatherCondition(weatherId);

//          COMPLETED (2) Use NotificationCompat.Builder to begin building the notification
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                    .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
                    .setSmallIcon(smallArtResourceId)
                    .setLargeIcon(largeIcon)
                    .setContentTitle(notificationTitle)
                    .setContentText(notificationText)
                    .setAutoCancel(true);

//          COMPLETED (3) Create an Intent with the proper URI to start the DetailActivity
            /*
             * This Intent will be triggered when the user clicks the notification. In our case,
             * we want to open Sunshine to the DetailActivity to display the newly updated weather.
             */
            Intent detailIntentForToday = new Intent(context, DetailActivity.class);
            detailIntentForToday.setData(todaysWeatherUri);

//          COMPLETED (4) Use TaskStackBuilder to create the proper PendingINtent
            TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
            taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday);
            PendingIntent resultPendingIntent = taskStackBuilder
                    .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

//          COMPLETED (5) Set the content Intent of the NotificationBuilder
            notificationBuilder.setContentIntent(resultPendingIntent);

//          COMPLETED (6) Get a reference to the NotificationManager
            NotificationManager notificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);

//          COMPLETED (7) Notify the user with the ID WEATHER_NOTIFICATION_ID
            /* WEATHER_NOTIFICATION_ID allows you to update or cancel the notification later on */
            notificationManager.notify(WEATHER_NOTIFICATION_ID, notificationBuilder.build());

//          COMPLETED (8) Save the time at which the notification occurred using SunshinePreferences
            /*
             * Since we just showed a notification, save the current time. That way, we can check
             * next time the weather is refreshed if we should show another notification.
             */
            SunshinePreferences.saveLastNotificationTime(context, System.currentTimeMillis());
        }

        /* Always close your cursor when you're done with it to avoid wasting resources. */
        todayWeatherCursor.close();
    }
 
Example 15
Source File: BirthdayService.java    From Klyph with MIT License 4 votes vote down vote up
private void sendNotification(List<GraphObject> list)
{
	if (service.get() == null)
		return;

	Service s = service.get();

	final NotificationCompat.Builder builder = new NotificationCompat.Builder(service.get()).setSmallIcon(R.drawable.ic_notification)
			.setContentTitle(s.getString(R.string.app_large_name)).setContentText(s.getString(R.string.friends_birthday_today, list.size()))
			.setTicker(s.getString(R.string.friends_birthday_today, list.size())).setOnlyAlertOnce(true).setAutoCancel(true)
			.setOnlyAlertOnce(true);

	builder.setDefaults(android.app.Notification.DEFAULT_SOUND | android.app.Notification.DEFAULT_VIBRATE
						| android.app.Notification.FLAG_ONLY_ALERT_ONCE);

	// Big notification style
	if (list.size() > 1)
	{
		builder.setNumber(list.size());
		NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

		inboxStyle.setBigContentTitle(s.getString(R.string.friends_birthday_today, list.size()));

		for (int i = 0; i < list.size(); i++)
		{
			inboxStyle.addLine(((Friend) list.get(i)).getName());
		}

		builder.setStyle(inboxStyle);
	}

	TaskStackBuilder stackBuilder = TaskStackBuilder.create(service.get());

	Intent resultIntent = new Intent(service.get(), MainActivity.class);
	resultIntent.putExtra(KlyphBundleExtras.SHOW_BIRTHDAYS, true);
	// stackBuilder.addParentStack(MainActivity.class);

	// Adds the Intent to the top of the stack
	stackBuilder.addNextIntentWithParentStack(resultIntent);

	int intentCode = (int) Math.round(Math.random() * 1000000);

	// Gets a PendingIntent containing the entire back stack
	PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(intentCode, PendingIntent.FLAG_UPDATE_CURRENT);
	builder.setContentIntent(resultPendingIntent);

	if (builder != null)
	{
		final NotificationManager mNotificationManager = (NotificationManager) s.getSystemService(Context.NOTIFICATION_SERVICE);

		final String tag = AttrUtil.getString(service.get(), R.string.app_name) + Math.round(Math.random() * 1000000);
		final int id = 0;

		// pair (tag, id) must be unique
		// because n.getObject_id() may not be converted to an int
		// tag is the unique key
		mNotificationManager.notify(tag, id, builder.build());

	}
}
 
Example 16
Source File: NotificationUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs and displays a notification for the newly updated weather for today.
 *
 * @param context Context used to query our ContentProvider and use various Utility methods
 */
public static void notifyUserOfNewWeather(Context context) {

    /* Build the URI for today's weather in order to show up to date data in notification */
    Uri todaysWeatherUri = WeatherContract.WeatherEntry
            .buildWeatherUriWithDate(SunshineDateUtils.normalizeDate(System.currentTimeMillis()));

    /*
     * The MAIN_FORECAST_PROJECTION array passed in as the second parameter is defined in our WeatherContract
     * class and is used to limit the columns returned in our cursor.
     */
    Cursor todayWeatherCursor = context.getContentResolver().query(
            todaysWeatherUri,
            WEATHER_NOTIFICATION_PROJECTION,
            null,
            null,
            null);

    /*
     * If todayWeatherCursor is empty, moveToFirst will return false. If our cursor is not
     * empty, we want to show the notification.
     */
    if (todayWeatherCursor.moveToFirst()) {

        /* Weather ID as returned by API, used to identify the icon to be used */
        int weatherId = todayWeatherCursor.getInt(INDEX_WEATHER_ID);
        double high = todayWeatherCursor.getDouble(INDEX_MAX_TEMP);
        double low = todayWeatherCursor.getDouble(INDEX_MIN_TEMP);

        Resources resources = context.getResources();
        int largeArtResourceId = SunshineWeatherUtils
                .getLargeArtResourceIdForWeatherCondition(weatherId);

        Bitmap largeIcon = BitmapFactory.decodeResource(
                resources,
                largeArtResourceId);

        String notificationTitle = context.getString(R.string.app_name);

        String notificationText = getNotificationText(context, weatherId, high, low);

        /* getSmallArtResourceIdForWeatherCondition returns the proper art to show given an ID */
        int smallArtResourceId = SunshineWeatherUtils
                .getSmallArtResourceIdForWeatherCondition(weatherId);

        /*
         * NotificationCompat Builder is a very convenient way to build backward-compatible
         * notifications. In order to use it, we provide a context and specify a color for the
         * notification, a couple of different icons, the title for the notification, and
         * finally the text of the notification, which in our case in a summary of today's
         * forecast.
         */
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setColor(ContextCompat.getColor(context,R.color.colorPrimary))
                .setSmallIcon(smallArtResourceId)
                .setLargeIcon(largeIcon)
                .setContentTitle(notificationTitle)
                .setContentText(notificationText)
                .setAutoCancel(true);

        /*
         * This Intent will be triggered when the user clicks the notification. In our case,
         * we want to open Sunshine to the DetailActivity to display the newly updated weather.
         */
        Intent detailIntentForToday = new Intent(context, DetailActivity.class);
        detailIntentForToday.setData(todaysWeatherUri);

        TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
        taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday);
        PendingIntent resultPendingIntent = taskStackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        notificationBuilder.setContentIntent(resultPendingIntent);

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

        /* WEATHER_NOTIFICATION_ID allows you to update or cancel the notification later on */
        notificationManager.notify(WEATHER_NOTIFICATION_ID, notificationBuilder.build());

        /*
         * Since we just showed a notification, save the current time. That way, we can check
         * next time the weather is refreshed if we should show another notification.
         */
        SunshinePreferences.saveLastNotificationTime(context, System.currentTimeMillis());
    }

    /* Always close your cursor when you're done with it to avoid wasting resources. */
    todayWeatherCursor.close();
}
 
Example 17
Source File: NotificationUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs and displays a notification for the newly updated weather for today.
 *
 * @param context Context used to query our ContentProvider and use various Utility methods
 */
public static void notifyUserOfNewWeather(Context context) {

    /* Build the URI for today's weather in order to show up to date data in notification */
    Uri todaysWeatherUri = WeatherContract.WeatherEntry
            .buildWeatherUriWithDate(SunshineDateUtils.normalizeDate(System.currentTimeMillis()));

    /*
     * The MAIN_FORECAST_PROJECTION array passed in as the second parameter is defined in our WeatherContract
     * class and is used to limit the columns returned in our cursor.
     */
    Cursor todayWeatherCursor = context.getContentResolver().query(
            todaysWeatherUri,
            WEATHER_NOTIFICATION_PROJECTION,
            null,
            null,
            null);

    /*
     * If todayWeatherCursor is empty, moveToFirst will return false. If our cursor is not
     * empty, we want to show the notification.
     */
    if (todayWeatherCursor.moveToFirst()) {

        /* Weather ID as returned by API, used to identify the icon to be used */
        int weatherId = todayWeatherCursor.getInt(INDEX_WEATHER_ID);
        double high = todayWeatherCursor.getDouble(INDEX_MAX_TEMP);
        double low = todayWeatherCursor.getDouble(INDEX_MIN_TEMP);

        Resources resources = context.getResources();
        int largeArtResourceId = SunshineWeatherUtils
                .getLargeArtResourceIdForWeatherCondition(weatherId);

        Bitmap largeIcon = BitmapFactory.decodeResource(
                resources,
                largeArtResourceId);

        String notificationTitle = context.getString(R.string.app_name);

        String notificationText = getNotificationText(context, weatherId, high, low);

        /* getSmallArtResourceIdForWeatherCondition returns the proper art to show given an ID */
        int smallArtResourceId = SunshineWeatherUtils
                .getSmallArtResourceIdForWeatherCondition(weatherId);

        /*
         * NotificationCompat Builder is a very convenient way to build backward-compatible
         * notifications. In order to use it, we provide a context and specify a color for the
         * notification, a couple of different icons, the title for the notification, and
         * finally the text of the notification, which in our case in a summary of today's
         * forecast.
         */
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setColor(ContextCompat.getColor(context,R.color.colorPrimary))
                .setSmallIcon(smallArtResourceId)
                .setLargeIcon(largeIcon)
                .setContentTitle(notificationTitle)
                .setContentText(notificationText)
                .setAutoCancel(true);

        /*
         * This Intent will be triggered when the user clicks the notification. In our case,
         * we want to open Sunshine to the DetailActivity to display the newly updated weather.
         */
        Intent detailIntentForToday = new Intent(context, DetailActivity.class);
        detailIntentForToday.setData(todaysWeatherUri);

        TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
        taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday);
        PendingIntent resultPendingIntent = taskStackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        notificationBuilder.setContentIntent(resultPendingIntent);

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

        /* WEATHER_NOTIFICATION_ID allows you to update or cancel the notification later on */
        notificationManager.notify(WEATHER_NOTIFICATION_ID, notificationBuilder.build());

        /*
         * Since we just showed a notification, save the current time. That way, we can check
         * next time the weather is refreshed if we should show another notification.
         */
        SunshinePreferences.saveLastNotificationTime(context, System.currentTimeMillis());
    }

    /* Always close your cursor when you're done with it to avoid wasting resources. */
    todayWeatherCursor.close();
}
 
Example 18
Source File: NotificationUtils.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs and displays a notification for the newly updated weather for today.
 *
 * @param context Context used to query our ContentProvider and use various Utility methods
 */
public static void notifyUserOfNewWeather(Context context) {

    /* Build the URI for today's weather in order to show up to date data in notification */
    Uri todaysWeatherUri = WeatherContract.WeatherEntry
            .buildWeatherUriWithDate(SunshineDateUtils.normalizeDate(System.currentTimeMillis()));

    /*
     * The MAIN_FORECAST_PROJECTION array passed in as the second parameter is defined in our WeatherContract
     * class and is used to limit the columns returned in our cursor.
     */
    Cursor todayWeatherCursor = context.getContentResolver().query(
            todaysWeatherUri,
            WEATHER_NOTIFICATION_PROJECTION,
            null,
            null,
            null);

    /*
     * If todayWeatherCursor is empty, moveToFirst will return false. If our cursor is not
     * empty, we want to show the notification.
     */
    if (todayWeatherCursor.moveToFirst()) {

        /* Weather ID as returned by API, used to identify the icon to be used */
        int weatherId = todayWeatherCursor.getInt(INDEX_WEATHER_ID);
        double high = todayWeatherCursor.getDouble(INDEX_MAX_TEMP);
        double low = todayWeatherCursor.getDouble(INDEX_MIN_TEMP);

        Resources resources = context.getResources();
        int largeArtResourceId = SunshineWeatherUtils
                .getLargeArtResourceIdForWeatherCondition(weatherId);

        Bitmap largeIcon = BitmapFactory.decodeResource(
                resources,
                largeArtResourceId);

        String notificationTitle = context.getString(R.string.app_name);

        String notificationText = getNotificationText(context, weatherId, high, low);

        /* getSmallArtResourceIdForWeatherCondition returns the proper art to show given an ID */
        int smallArtResourceId = SunshineWeatherUtils
                .getSmallArtResourceIdForWeatherCondition(weatherId);

        /*
         * NotificationCompat Builder is a very convenient way to build backward-compatible
         * notifications. In order to use it, we provide a context and specify a color for the
         * notification, a couple of different icons, the title for the notification, and
         * finally the text of the notification, which in our case in a summary of today's
         * forecast.
         */
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setColor(ContextCompat.getColor(context,R.color.colorPrimary))
                .setSmallIcon(smallArtResourceId)
                .setLargeIcon(largeIcon)
                .setContentTitle(notificationTitle)
                .setContentText(notificationText)
                .setAutoCancel(true);

        /*
         * This Intent will be triggered when the user clicks the notification. In our case,
         * we want to open Sunshine to the DetailActivity to display the newly updated weather.
         */
        Intent detailIntentForToday = new Intent(context, DetailActivity.class);
        detailIntentForToday.setData(todaysWeatherUri);

        TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
        taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday);
        PendingIntent resultPendingIntent = taskStackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        notificationBuilder.setContentIntent(resultPendingIntent);

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

        /* WEATHER_NOTIFICATION_ID allows you to update or cancel the notification later on */
        notificationManager.notify(WEATHER_NOTIFICATION_ID, notificationBuilder.build());

        /*
         * Since we just showed a notification, save the current time. That way, we can check
         * next time the weather is refreshed if we should show another notification.
         */
        SunshinePreferences.saveLastNotificationTime(context, System.currentTimeMillis());
    }

    /* Always close your cursor when you're done with it to avoid wasting resources. */
    todayWeatherCursor.close();
}
 
Example 19
Source File: KlyphNotification.java    From Klyph with MIT License 3 votes vote down vote up
public static void sendNotification(Context context, Builder builder)
{
	TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

	Intent resultIntent = new Intent(context, MainActivity.class);
	resultIntent.putExtra(KlyphBundleExtras.SHOW_NOTIFICATION_MENU, true);

	stackBuilder.addNextIntentWithParentStack(resultIntent);

	int intentCode = (int) Math.round(Math.random() * 1000000);

	// Gets a PendingIntent containing the entire back stack
	PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(intentCode, PendingIntent.FLAG_UPDATE_CURRENT);
	builder.setContentIntent(resultPendingIntent);
	
	Intent intent = new Intent(context, NotificationGroupDeletedReceiver.class);
	builder.setDeleteIntent(PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

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

	final String tag = context.getPackageName() + "_grouped";
	final int id = 0;

	// pair (tag, id) must be unique
	// tag is the unique key
	mNotificationManager.notify(tag, id, builder.build());

}
 
Example 20
Source File: BaseActivity.java    From privacy-friendly-netmonitor with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Enables back navigation for activities that are launched from the NavBar. See
 * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
 *
 * @param intent
 */
private void createBackStack(Intent intent) {
    TaskStackBuilder builder = TaskStackBuilder.create(this);
    builder.addNextIntentWithParentStack(intent);
    builder.startActivities();
}