C# 为什么我只能在android通知中按一次播放/暂停按钮?
我已经为我的媒体应用程序创建了一个通知,允许用户播放/暂停他们的音频。但是现在他们只能按通知中的按钮一次。。我如何获得它,以便用户可以连续点击播放/暂停按钮 我试着在我的活动中做一个新的通知,作为一种刷新信息的方式,但还没有得到多少结果 构建通知C# 为什么我只能在android通知中按一次播放/暂停按钮?,c#,android,xamarin,android-intent,notifications,C#,Android,Xamarin,Android Intent,Notifications,我已经为我的媒体应用程序创建了一个通知,允许用户播放/暂停他们的音频。但是现在他们只能按通知中的按钮一次。。我如何获得它,以便用户可以连续点击播放/暂停按钮 我试着在我的活动中做一个新的通知,作为一种刷新信息的方式,但还没有得到多少结果 构建通知 public void Init(DabPlayer Player, bool IntegrateWithLockScreen) { player = Player; var mSessi
public void Init(DabPlayer Player, bool IntegrateWithLockScreen)
{
player = Player;
var mSession = new MediaSessionCompat(Application.Context, "MusicService");
mSession.SetFlags(MediaSessionCompat.FlagHandlesMediaButtons | MediaSessionCompat.FlagHandlesTransportControls);
var controller = mSession.Controller;
var description = GlobalResources.playerPodcast;
if (IntegrateWithLockScreen)
{
/* SET UP LOCK SCREEN */
CreateNotificationChannel();
player.EpisodeDataChanged += (sender, e) =>
{
// Set up an intent so that tapping the notifications returns to this app:
Intent intent = new Intent(Application.Context, typeof(MainActivity));
Intent playPauseIntent = new Intent(Application.Context, typeof(SecondActivity));
// Create a PendingIntent; we're only using one PendingIntent (ID = 0):
const int pendingIntentId = 0;
const int firstPendingIntentId = 1;
PendingIntent firstPendingIntent =
PendingIntent.GetActivity(Application.Context, firstPendingIntentId, intent, PendingIntentFlags.OneShot);
PendingIntent pendingIntent =
PendingIntent.GetActivity(Application.Context, pendingIntentId, playPauseIntent, PendingIntentFlags.OneShot);
// Build the notification:
var builder = new NotificationCompat.Builder(Application.Context, CHANNEL_ID)
.SetStyle(new Android.Support.V4.Media.App.NotificationCompat.MediaStyle()
.SetMediaSession(mSession.SessionToken)
.SetShowActionsInCompactView(0, 1))
.SetVisibility(NotificationCompat.VisibilityPublic)
.SetContentIntent(firstPendingIntent) // Start up this activity when the user clicks the intent.
.SetDeleteIntent(MediaButtonReceiver.BuildMediaButtonPendingIntent(Application.Context, PlaybackState.ActionStop))
.SetSmallIcon(Resource.Drawable.app_icon) // This is the icon to display
.AddAction(Resource.Drawable.ic_media_pause_dark, "Pause", pendingIntent)
.AddAction(Resource.Drawable.ic_media_play_dark, "Next", pendingIntent)
.SetContentText(GlobalResources.playerPodcast.EpisodeTitle)
.SetContentTitle(GlobalResources.playerPodcast.ChannelTitle);
// Finally, publish the notification:
var notificationManager = NotificationManagerCompat.From(Application.Context);
notificationManager.Notify(NOTIFICATION_ID, builder.Build());
};
player.EpisodeProgressChanged += (object sender, EventArgs e) =>
{
};
}
然后我的活动看起来像这样
[Activity]
public class SecondActivity : Activity
{
DabPlayer player = GlobalResources.playerPodcast;
EpisodeViewModel Episode;
DroidDabNativePlayer droid = new DroidDabNativePlayer();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
if (player.IsReady)
{
if (player.IsPlaying)
{
player.Pause();
droid.Init(player, true);
}
else
{
player.Play();
droid.Init(player, true);
}
}
else
{
if (player.Load(Episode.Episode))
{
player.Play();
droid.Init(player, true);
}
else
{
//DisplayAlert("Episode Unavailable", "The episode you are attempting to play is currently unavailable. Please try again later.", "OK");
}
}
Finish();
}
}
我还想按下一个按钮,让它根据状态在显示暂停和播放之间切换,如果有人想给我指出正确的方向
感谢您的帮助!谢谢大家!
带有OneShot
的悬挂式帐篷只能使用一次
指示此PendingEvent只能使用一次的标志。
如果设置了,则在对其调用send()后,它将自动取消,并且将来通过它发送的任何尝试都将失败
使用不带标志的
pendingent
(允许用户多次按下按钮并发送多个意图),或者在每次请求pendingent
时使用新id,以便在每次更新通知时按下按钮一次。什么意思?他们只能按一次播放?第二次会发生什么?刷新通知的结果是什么?它不会响应第二次单击事件。他们第二次点击后什么也没发生。刷新通知后,我无法再次单击按钮。现在有两个按钮在执行相同的操作,只是外观不同,但如果我单击其中一个按钮,我甚至无法单击另一个按钮。在第一次事件之后,它似乎只是扼杀了按钮的交互。它像一个符咒一样工作!非常感谢。