示例#1
0
  private void showNotification() {
    String[] projection =
        new String[] {
          PodcastProvider.COLUMN_ID,
          PodcastProvider.COLUMN_TITLE,
          PodcastProvider.COLUMN_SUBSCRIPTION_ID,
          PodcastProvider.COLUMN_SUBSCRIPTION_TITLE,
          PodcastProvider.COLUMN_SUBSCRIPTION_THUMBNAIL,
        };
    Cursor c =
        getContentResolver()
            .query(PodcastProvider.ACTIVE_PODCAST_URI, projection, null, null, null);
    if (c == null) return;
    if (c.isAfterLast()) {
      c.close();
      return;
    }
    PodcastCursor podcast = new PodcastCursor(c);

    // both paths use the pendingintent
    Intent showIntent = new Intent(this, MainActivity.class);
    PendingIntent showPendingIntent = PendingIntent.getActivity(this, 0, showIntent, 0);

    NotificationCompat.Builder builder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setWhen(0)
            .setContentTitle(podcast.getTitle())
            .setContentText(podcast.getSubscriptionTitle())
            .setContentIntent(showPendingIntent)
            .setOngoing(true)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    // set up pause intent
    Intent pauseIntent = new Intent(this, PlayerService.class);
    // use data to make intent unique
    pauseIntent.setData(Uri.parse("podax://playercommand/playpause"));
    pauseIntent.putExtra(Constants.EXTRA_PLAYER_COMMAND, Constants.PLAYER_COMMAND_PLAYPAUSE);
    pauseIntent.putExtra(Constants.EXTRA_PLAYER_COMMAND_ARG, Constants.PAUSE_MEDIABUTTON);
    PendingIntent pausePendingIntent = PendingIntent.getService(this, 0, pauseIntent, 0);

    // set up forward intent
    Intent forwardIntent = new Intent(this, ActivePodcastReceiver.class);
    forwardIntent.setData(Constants.ACTIVE_PODCAST_DATA_FORWARD);
    PendingIntent forwardPendingIntent = PendingIntent.getService(this, 0, forwardIntent, 0);

    Bitmap subscriptionBitmap =
        Helper.getCachedImage(this, podcast.getSubscriptionThumbnailUrl(), 128, 128);
    if (subscriptionBitmap != null) builder.setLargeIcon(subscriptionBitmap);

    if (PlayerStatus.getPlayerState(this) == PlayerStates.PLAYING)
      builder.addAction(
          R.drawable.ic_media_pause_normal, getString(R.string.pause), pausePendingIntent);
    else
      builder.addAction(
          R.drawable.ic_media_play_normal, getString(R.string.play), pausePendingIntent);
    builder.addAction(
        R.drawable.ic_media_ff_normal, getString(R.string.fast_forward), forwardPendingIntent);

    Notification notification = builder.build();
    startForeground(Constants.NOTIFICATION_PLAYING, notification);

    c.close();
  }