我正在使用RemoteViews为我的播放器构建通知。 我希望用户能够删除通知(来自锁屏和解锁时)并在完成后停止播放。我怎样才能实现这一目标? 这是我构建它的代码:
public void buildNotification(String action) {
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification_layout);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(ACTION_MAIN);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent previousIntent = new Intent(this, MediaPlayerService.class);
previousIntent.setAction(ACTION_PREVIOUS);
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, previousIntent, 0);
Intent rewindIntent = new Intent(this, MediaPlayerService.class);
previousIntent.setAction(ACTION_REWIND);
PendingIntent prewindIntent = PendingIntent.getService(this, 0, rewindIntent, 0);
Intent playIntent = new Intent(this, MediaPlayerService.class);
playIntent.setAction(action);
PendingIntent pplayIntent = PendingIntent.getService(this, 0, playIntent, 0);
Intent forwardIntent = new Intent(this, MediaPlayerService.class);
playIntent.setAction(ACTION_FAST_FORWARD);
PendingIntent pforwardIntent = PendingIntent.getService(this, 0, forwardIntent, 0);
Intent nextIntent = new Intent(this, MediaPlayerService.class);
nextIntent.setAction(ACTION_NEXT);
PendingIntent pnextIntent = PendingIntent.getService(this, 0, nextIntent, 0);
views.setOnClickPendingIntent(R.id.imgbtn_prev, ppreviousIntent);
views.setOnClickPendingIntent(R.id.imgbtn_rewind, prewindIntent);
views.setOnClickPendingIntent(R.id.imgbtn_pause, pplayIntent);
views.setOnClickPendingIntent(R.id.imgbtn_forward, pforwardIntent);
views.setOnClickPendingIntent(R.id.imgbtn_next, pnextIntent);
//views.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);
MainActivity m = MainActivity.sInstance;
views.setImageViewResource(R.id.imgbtn_prev, android.R.drawable.ic_media_previous);
views.setImageViewResource(R.id.imgbtn_rewind, android.R.drawable.ic_media_rew);
views.setImageViewResource(R.id.imgbtn_pause, action.equals(ACTION_PAUSE) ?
android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play);
views.setImageViewResource(R.id.imgbtn_forward, android.R.drawable.ic_media_ff);
views.setImageViewResource(R.id.imgbtn_next, android.R.drawable.ic_media_next);
String s = Player.getInstance().song.getName();
views.setTextViewText(R.id.filename, s.substring(0,s.length()-4));
Notification status = new Notification.Builder(this).build();
status.contentView = views;
status.flags = Notification.FLAG_ONGOING_EVENT;
status.icon = R.mipmap.ic_launcher;
status.contentIntent = pendingIntent;
startForeground(1, status);
}
btw,contentView
和icon
似乎已被弃用,我应该怎么做呢?
答案 0 :(得分:0)
试试这个:
public Notification buildNotification(String action) {
//...
Notification status = new Notification.Builder(this).build();
status.contentView = views;
status.flags = Notification.FLAG_ONGOING_EVENT;
status.icon = R.mipmap.ic_launcher;
status.contentIntent = pendingIntent;
return status;
}
播放开始通知,如
startForeground(1, buildNotification):
暂停时,只需通知,然后就可以删除。
notificationManager.notify(1, buildNotification);