Question

Using jellybean. Here play notification bar and pause notification bar. Different only layout resource ID.

show play button :

public static void showNotify(Service $context)
{
  Resources res = $context.getResources();

  Intent playInent = new Intent($context, MainService.class);
  playInent.setAction("play");
  PendingIntent playPendingIntent = PendingIntent.getService($context, 0, playInent, PendingIntent.FLAG_UPDATE_CURRENT);

  Intent prevIntent = new Intent($context, MainService.class);
  prevIntent.setAction("prev");
  PendingIntent prevPendingIntent = PendingIntent.getService($context, 1, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  Intent nextIntent = new Intent($context, MainService.class);
  nextIntent.setAction("next");
  PendingIntent nextPendingIntent = PendingIntent.getService($context, 2, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  Intent offIntent = new Intent($context, MainService.class);
  offIntent.setAction("off");
  PendingIntent offPendingIntent = PendingIntent.getService($context, 3, offIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  RemoteViews remoteViews = new RemoteViews($context.getPackageName(), R.layout.noti_view);
  remoteViews.setOnClickPendingIntent(R.id.button1, playPendingIntent);
  remoteViews.setOnClickPendingIntent(R.id.button2, prevPendingIntent);
  remoteViews.setOnClickPendingIntent(R.id.button3, nextPendingIntent);
  remoteViews.setOnClickPendingIntent(R.id.button4, offPendingIntent);

  RemoteViews bigView = new RemoteViews($context.getPackageName(), R.layout.noti_big_view);
  bigView.setOnClickPendingIntent(R.id.button1, playPendingIntent);
  bigView.setOnClickPendingIntent(R.id.button2, prevPendingIntent);
  bigView.setOnClickPendingIntent(R.id.button3, nextPendingIntent);
  bigView.setOnClickPendingIntent(R.id.button4, offPendingIntent);

  NotificationCompat.Builder builder = new NotificationCompat.Builder($context).setSmallIcon(R.drawable.ic_launcher)
                                                                               .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                                                                               .setTicker(res.getString(R.string.app_name))
                                                                               .setWhen(System.currentTimeMillis())
                                                                               .setContentTitle(res.getString(R.string.app_name));

  builder.setContent(remoteViews);
  Notification notification = builder.build();

  notification.bigContentView = bigView;

  //    notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
  $context.startForeground(ID_REMOTSERVICE, notification);
}

and show pause button :

public static void pauseNotify(Service $context)
{
  Resources res = $context.getResources();

  Intent playInent = new Intent($context, MainService.class);
  playInent.setAction("pause");
  PendingIntent playPendingIntent = PendingIntent.getService($context, 0, playInent, PendingIntent.FLAG_UPDATE_CURRENT);

  Intent prevIntent = new Intent($context, MainService.class);
  prevIntent.setAction("prev");
  PendingIntent prevPendingIntent = PendingIntent.getService($context, 1, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  Intent nextIntent = new Intent($context, MainService.class);
  nextIntent.setAction("next");
  PendingIntent nextPendingIntent = PendingIntent.getService($context, 2, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  Intent offIntent = new Intent($context, MainService.class);
  offIntent.setAction("off");
  PendingIntent offPendingIntent = PendingIntent.getService($context, 3, offIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  RemoteViews remoteViews = new RemoteViews($context.getPackageName(), R.layout.noti_view_pause);
  remoteViews.setOnClickPendingIntent(R.id.button1, playPendingIntent);
  remoteViews.setOnClickPendingIntent(R.id.button2, prevPendingIntent);
  remoteViews.setOnClickPendingIntent(R.id.button3, nextPendingIntent);
  remoteViews.setOnClickPendingIntent(R.id.button4, offPendingIntent);

  RemoteViews bigView = new RemoteViews($context.getPackageName(), R.layout.noti_big_view_pause);
  bigView.setOnClickPendingIntent(R.id.button1, playPendingIntent);
  bigView.setOnClickPendingIntent(R.id.button2, prevPendingIntent);
  bigView.setOnClickPendingIntent(R.id.button3, nextPendingIntent);
  bigView.setOnClickPendingIntent(R.id.button4, offPendingIntent);

  NotificationCompat.Builder builder = new NotificationCompat.Builder($context).setSmallIcon(R.drawable.ic_launcher)
                                                                               .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                                                                               .setTicker(res.getString(R.string.app_name))
                                                                               .setWhen(System.currentTimeMillis())
                                                                               .setContentTitle(res.getString(R.string.app_name));

  builder.setContent(remoteViews);
  Notification notification = builder.build();
  notification.bigContentView = bigView;

//    notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
  $context.startForeground(ID_REMOTSERVICE, notification);
}

This code is change notification bar but with blink and slowly. How to make similar media control button of Play Music app?

Was it helpful?

Solution

I found solution myself.

public class NotifyUtil
{
  private final static int ID_REMOTSERVICE = 1;
  private RemoteViews _smallView, _bigView;
  private Notification _notification;
  private Intent _playIntent;
  private PendingIntent _playPendingIntent;


  public NotifyUtil(Service $context)
  {
    _playIntent = new Intent($context, MainService.class);
    _playIntent.setAction("play");
    _playPendingIntent = PendingIntent.getService($context, 0, _playIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent prevIntent = new Intent($context, MainService.class);
    prevIntent.setAction("prev");
    PendingIntent prevPendingIntent = PendingIntent.getService($context, 1, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent nextIntent = new Intent($context, MainService.class);
    nextIntent.setAction("next");
    PendingIntent nextPendingIntent = PendingIntent.getService($context, 2, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent offIntent = new Intent($context, MainService.class);
    offIntent.setAction("off");
    PendingIntent offPendingIntent = PendingIntent.getService($context, 3, offIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    _smallView = new RemoteViews($context.getPackageName(), R.layout.noti_view);
    _smallView.setOnClickPendingIntent(R.id.button1, _playPendingIntent);
    _smallView.setOnClickPendingIntent(R.id.button2, prevPendingIntent);
    _smallView.setOnClickPendingIntent(R.id.button3, nextPendingIntent);
    _smallView.setOnClickPendingIntent(R.id.button4, offPendingIntent);

    _bigView = new RemoteViews($context.getPackageName(), R.layout.noti_big_view);
    _bigView.setOnClickPendingIntent(R.id.button1, _playPendingIntent);
    _bigView.setOnClickPendingIntent(R.id.button2, prevPendingIntent);
    _bigView.setOnClickPendingIntent(R.id.button3, nextPendingIntent);
    _bigView.setOnClickPendingIntent(R.id.button4, offPendingIntent);

    Resources res = $context.getResources();
    NotificationCompat.Builder builder = new NotificationCompat.Builder($context).setSmallIcon(R.drawable.ic_launcher)
                                                                                 .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                                                                                 .setTicker(res.getString(R.string.app_name))
                                                                                 .setContentTitle(res.getString(R.string.app_name));
    _notification = builder.build();
    _notification.contentView = _smallView;
    _notification.bigContentView = _bigView;
    _notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
  }


  public void showNotify(Service $context)
  {
    _smallView.setImageViewResource(R.id.button1, android.R.drawable.ic_media_play);
    _bigView.setImageViewResource(R.id.button1, android.R.drawable.ic_media_play);

    _playIntent.setAction("play");
    _playPendingIntent = PendingIntent.getService($context, 0, _playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    _smallView.setOnClickPendingIntent(R.id.button1, _playPendingIntent);
    _bigView.setOnClickPendingIntent(R.id.button1, _playPendingIntent);

    $context.startForeground(ID_REMOTSERVICE, _notification);
  }


  public void pauseNotify(Service $context)
  {
    _smallView.setImageViewResource(R.id.button1, android.R.drawable.ic_media_pause);
    _bigView.setImageViewResource(R.id.button1, android.R.drawable.ic_media_pause);

    _playIntent.setAction("pause");
    _playPendingIntent = PendingIntent.getService($context, 0, _playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    _smallView.setOnClickPendingIntent(R.id.button1, _playPendingIntent);
    _bigView.setOnClickPendingIntent(R.id.button1, _playPendingIntent);

    $context.startForeground(ID_REMOTSERVICE, _notification);
  }
}

then

NotifyUtil noti = new NotifyUtil();
noti.showNotify(context);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top