使用果冻豆。这里播放通知栏和暂停通知栏。仅不同的布局资源ID。
显示播放按钮:

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

显示暂停按钮:
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);
}

此代码是更改通知栏,但闪烁缓慢。如何制作播放音乐应用程序的类似媒体控制按钮?

最佳答案

我自己找到了解决办法。

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

然后
NotifyUtil noti = new NotifyUtil();
noti.showNotify(context);

09-10 01:04