当用户按下暂停按钮时,试图弄清楚为什么我的onGoing通知会消失。我正在尝试实施类似于Spotify持续通知的媒体播放器控制方案。我正在修改Androids Sample项目“RandomMusicPlayer”。我已经尝试将setAutoCancel设置为false。
下列相关代码:
/** Updates the notification. */
@SuppressWarnings("deprecation")
private void updateNotification(String text, String artist, boolean showticker) {
Notification notification = buildNotification(text, artist, showticker);
if(notification!=null){
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
/**
* Configures service as a foreground service. A foreground service is a service that's doing
* something the user is actively aware of (such as playing music), and must appear to the
* user as a notification. That's why we create the notification here.
*/
@SuppressWarnings("deprecation")
private void setUpAsForeground(String text, String artist, boolean showticker) {
Notification notification = buildNotification(text, artist, showticker);
if(notification != null){
startForeground(NOTIFICATION_ID, notification);
}
}
/**
* Called when we want to build our notification
*/
private Notification buildNotification(String title, String artist, boolean showticker){
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), HomeActivity.class).setAction(ACTION_OPEN_STREAM_FRAG),
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingStopIntent = PendingIntent.getService(this, 118, new Intent(MusicService.ACTION_STOP), PendingIntent.FLAG_NO_CREATE);
PendingIntent pendingPauseIntent = PendingIntent.getService(this, 119, new Intent(MusicService.ACTION_PAUSE), PendingIntent.FLAG_NO_CREATE);
PendingIntent pendingPlayIntent = PendingIntent.getService(this, 120, new Intent(MusicService.ACTION_PLAY), PendingIntent.FLAG_NO_CREATE);
RemoteViews mNotificationView = new RemoteViews(getPackageName(),
R.layout.player_notification_view);
Notification mNotification = null;
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
mNotificationView.setImageViewResource(R.id.imageNotification, R.drawable.ic_launcher );
mNotificationView.setTextViewText(R.id.titleNotification, title);
mNotificationView.setTextColor(R.id.titleNotification, Color.WHITE);
mNotificationView.setTextViewText(R.id.textNotification, artist);
if(mState == State.Playing){
mNotificationView.setImageViewResource(R.id.control_one_button_notification, R.drawable.ic_action_playback_pause);
mNotificationView.setImageViewResource(R.id.control_two_button_notification, R.drawable.ic_action_playback_stop);
mNotificationView.setOnClickPendingIntent(R.id.control_one_button_notification, pendingPauseIntent);
mNotificationView.setOnClickPendingIntent(R.id.control_two_button_notification, pendingStopIntent);
}else if(mState == State.Paused){
mNotificationView.setImageViewResource(R.id.control_one_button_notification, R.drawable.ic_action_playback_play);
mNotificationView.setImageViewResource(R.id.control_two_button_notification, R.drawable.ic_action_playback_stop);
mNotificationView.setOnClickPendingIntent(R.id.control_one_button_notification, pendingPlayIntent);
mNotificationView.setOnClickPendingIntent(R.id.control_two_button_notification, pendingStopIntent);
}
builder.setContentIntent(pi).setSmallIcon(R.drawable.ic_launcher);
if(showticker){
builder.setTicker(title);
}else{
builder.setTicker(null);
}
builder.setAutoCancel(false).setContent(mNotificationView).setOngoing(true);
mNotification = builder.build();
return mNotification;
}
答案 0 :(得分:1)
不确定你是否这样做,但你应该在Service
中实现这个,你应该使用:
startForeground(NOTIFICATION_ID, mNotification);
我已经创建了一个应用程序来做这件事,请看看我是如何在这里解决的:https://github.com/slidese/SGU
具体来说,请看这个课程:https://github.com/slidese/SGU/blob/master/src/se/slide/sgu/AudioPlayer.java
答案 1 :(得分:0)
只需在pendingIntent
函数中编写代码即可NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(01);
//"01" is the id of your notification