在xamarin上注册媒体按钮不起作用

时间:2016-05-06 18:48:46

标签: android xamarin xamarin.android

我一直在尝试注册以从媒体按钮接收广播事件。我已成功完成java,现在我将它移植到Xamarin。但无论我尝试什么,它都没有记录任何东西。我已经实现了各种接收器,但出于某种原因,媒体按钮对我来说不起作用。

我试过了:

RegisterReceiver(new BroadcastReceivers.Media_Button(), new IntentFilter(Intent.ActionMediaButton));

我试过了:

ComponentName myEventReceiver = new ComponentName(this, nameof(Media_Button));
AudioManager myAudioManager = (AudioManager) this.GetSystemService(Context.AudioService);
myAudioManager.RegisterMediaButtonEventReceiver(myEventReceiver);
Intent mediaButtonIntent = new Intent(Intent.ActionMediaButton);
mediaButtonIntent.SetComponent(myEventReceiver);
PendingIntent mediaPendingIntent = PendingIntent.GetBroadcast(this, 0, mediaButtonIntent, 0);
// create and register the remote control client
RemoteControlClient myRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
myAudioManager.RegisterRemoteControlClient(myRemoteControlClient);

但没有成功。

媒体按钮类:

[BroadcastReceiver(Enabled = true, Label = "Media Button Receiver")]
[IntentFilter(new[] { "android.intent.action.MEDIA_BUTTON" })]
class Media_Button : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Console.WriteLine("----------Detected media button--------------");
    }
}

请注意,传递的上下文来自服务而非活动

0 个答案:

没有答案
相关问题