安卓系统:按下remoteView中的自定义按钮时,正在进行的通知将消失



试图弄清楚为什么当用户按下暂停按钮时,我的onGoing通知会消失。我正在尝试实现一个类似于Spotify正在进行的通知的媒体播放器控制方案。我正在修改Androids样本项目"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;
}

不确定您是否这样做,但您应该在Service中实现它,在那里您应该使用:

startForeground(NOTIFICATION_ID, mNotification);

我制作了一个应用程序,可以做到这一点,请看看我是如何在这里解决的:https://github.com/slidese/SGU

具体来说,看看这个类:https://github.com/slidese/SGU/blob/master/src/se/slide/sgu/AudioPlayer.java

只需在pendingIntent 函数中编写一段代码

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(01);
//"01" is the id of your notification

或参考链接如何停止通知

最新更新