android.support.v4.app.TaskStackBuilder#addNextIntentWithParentStack ( )源码实例Demo

下面列出了android.support.v4.app.TaskStackBuilder#addNextIntentWithParentStack ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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);
}
 
/**
 * 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();
    }
}
 
/**
 * @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);
}
 
/**
 * @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);
}
 
/**
 * @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);
}
 
/**
 * 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();
    }
}
 
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);
}
 
源代码8 项目: privacy-friendly-dame   文件: BaseActivity.java
/**
 * 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();
    }
}
 
/**
 * 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();
    }
}
 
源代码10 项目: privacy-friendly-weather   文件: BaseActivity.java
/**
 * 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();
    }
}
 
源代码11 项目: privacy-friendly-torchlight   文件: BaseActivity.java
/**
 * 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();
    }
}
 
源代码12 项目: privacy-friendly-pedometer   文件: BaseActivity.java
/**
 * 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();
    }
}
 
源代码13 项目: Villains-and-Heroes   文件: CharacterActivity.java
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);
    }
 
源代码14 项目: android-dev-challenge   文件: NotificationUtils.java
/**
     * 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();
    }
 
源代码15 项目: Klyph   文件: BirthdayService.java
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());

	}
}
 
源代码16 项目: android-dev-challenge   文件: NotificationUtils.java
/**
 * 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();
}
 
源代码17 项目: android-dev-challenge   文件: NotificationUtils.java
/**
 * 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();
}
 
源代码18 项目: android-dev-challenge   文件: NotificationUtils.java
/**
 * 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();
}
 
源代码19 项目: Klyph   文件: KlyphNotification.java
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());

}
 
源代码20 项目: privacy-friendly-netmonitor   文件: BaseActivity.java
/**
 * 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();
}